Converting Large String To HEX More Quick Any Help Please
Hello frndz how are u ?
i want to convert large string to HEX and that takes a lot of time (3-5 minutes) using the normal StringToHex Function posted some where here ..
Code:
Public Function StringToHex(ByVal StrToHex As String) As String
Dim strTemp As String
Dim strReturn As String
Dim i As Long
For i = 1 To Len(StrToHex)
strTemp = Hex$(Asc(Mid$(StrToHex, i, 1)))
If Len(strTemp) = 1 Then strTemp = "0" & strTemp
strReturn = strReturn & Space$(1) & strTemp
Next i
StringToHex = strReturn
End Function
i want you please to help me make it more quick .. :(
btw i knew that there is 2 ways to do that using api's or without ..
Re: Converting Large String To HEX More Quick Any Help Please
Most likely you're wasting huge amounts of time creating new strings and copying the contents of the old one into them. There was a thread similar to this one recently you might check out.
1 Attachment(s)
Re: Converting Large String To HEX More Quick Any Help Please
Converted my byte array versions to string versions in 10 minutes or so. Writing this post took me longer than making the changes...
HexStringToString
Input: any string containing any hex pairs – any single character or invalid character pair is simply ignored
Output: string where all the hex pairs are translated to their byte equivalent
This means you can call HexStringToString("41 00garb.age!4200WOOHOO...43.......00") and the output is "ABC", because the only valid hex pairs are: 41, 00, 42, 00, 43, 00
StringToHexString(Text[, Format[, Separator]][, LowerCase])
Text: any string
Format: a string containing a zero pair to represent hex characters and other characters to represent a single row of data
(default value: "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
Separator: a string separator between each Format row
(default value: vbNewLine)
Lowercase: True = hex pair characters are lower case (abcdef), False = upper case (ABCDEF)
(default value: True)
Okay, this one here is a very powerful function. You can give it almost anything and you get it formatted the way you want. If all you want is to have a space separator between each hex pair, you can call it like this:
StringToHexString("ABC&abc&åäö", "00", " ", False)
Output: "41 00 42 00 43 00 26 00 61 00 62 00 63 00 26 00 E5 00 E4 00 F6 00"
But you can also do this kind of stuff:
StringToHexString("ABC&abc&åäö", "0000 0000 0000 0000", , False)
Output:
Code:
4100 4200 4300 2600
6100 6200 6300 2600
E500 E400 F600
FINALLY: the code is fast compiled. Running in the IDE it doesn't do that well, there the code is interpreted from P-code on-the-fly and that isn't very efficient.
Re: Converting Large String To HEX More Quick Any Help Please
One more addition: if you don't want to preserve Unicode data then you can use StrConv to convert from/to ANSI.
strTest = StringToHexString(StrConv("ABC", vbFromUnicode))
' strTest = "41 42 43"
strTest = StrConv(HexStringToString("414243"), vbUnicode)
' strTest = "ABC"
This decreases performance though, because a new string is allocated each time StrConv is called and in addition all the bytes of that string are processed. However VB6 does this kind of conversion each time you write/read string to/from files or the native controls (label, textbox, command buttons etc.), which may confuse you if you're used to think one byte = one character when dealing with files.
Re: Converting Large String To HEX More Quick Any Help Please
you can try this as another alternative, designed for large numbers not sure how fast it is though:
Code:
Function DecToHex(ByVal x As Variant) As String
Dim y%: x = CDec(x)
Do
y = (x / 16 - Int(x / 16)) * 16
If y < 10 Then DecToHex = y & DecToHex Else DecToHex = Chr$(y + 55) & DecToHex
x = Int(x / 16)
Loop While x
End Function