Results 1 to 5 of 5

Thread: Converting Large String To HEX More Quick Any Help Please

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    1

    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 ..

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    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.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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&&#229;&#228;&#246;", "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&&#229;&#228;&#246;", "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.
    Attached Files Attached Files
    Last edited by Merri; Feb 12th, 2011 at 05:05 AM.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  5. #5
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width