Results 1 to 13 of 13

Thread: [RESOLVED] Convert Hex String to a normal String

Threaded View

  1. #12
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: [RESOLVED] Convert Hex String to a normal String

    I decided to have my own go at some string hacking. You can get the wrapper here: http://www.xbeat.net/vbspeed/i_Dope.htm

    (Scroll down, this is what you want
    modSafeArray by Paul
    wrapping tough string array hacks
    modSafeArray_Paul.zip, 1.147 bytes, 20.10.01 11:05:12
    vb Code:
    1. Public Function HexToAsc(hstr As String) As String
    2.     Static Table(255) As Byte
    3.     Dim X As Long, I As Long
    4.     Dim saHeader As SAFEARRAYHEADER
    5.     Dim hBa() As Byte 'store the string as byte array
    6.     ReDim nstr(Len(hstr) \ 2 - 1) As Byte
    7.    
    8.     ' table from characters "0123456789", "ABCDEF", "abcdef" to their numeric values (0 - 15)
    9.     If Table(49) = 0 Then
    10.         For I = 1 To 9: Table(48 + I) = I: Next
    11.         For I = 10 To 15: Table(55 + I) = I: Table(87 + I) = I: Next
    12.     End If
    13.    
    14.     RedimArray byteArray, LenB(hstr), saHeader, StrPtr(hstr), VarPtrArray(hBa)
    15.  
    16.     For X = 0 To LenB(hstr) - 3 Step 4
    17.       nstr(X \ 4) = Table(hBa(X)) * 16 + Table(hBa(X + 2)) 'bit shift and add
    18.     Next
    19.     DestroyArray VarPtrArray(hBa)
    20.     HexToAsc = StrConv(nstr, vbUnicode)
    21. End Function

    In my own tests it's now the fastest function here, but it could still be improved.

    In case you're wondering about the results:
    Code:
    Name                 | Timings        
    =========================================
    HexANSIToString      = 439.07  ms = 21.1
    HexANSIToStringFast2 = 29.21   ms = 1.404
    HexANSIToStringFast3 = 32.8225 ms = 1.578
    HexToAsc             = 20.8    ms = 1
    QuickHex2Str         = 30.4775 ms = 1.465
    
    Method:
    Input = 1.5 million HEX characters, output = 750,000 ASC characters
    Test was performed 4 times, and the mean taken.

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