|
-
May 25th, 2010, 02:57 PM
#12
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:
Public Function HexToAsc(hstr As String) As String
Static Table(255) As Byte
Dim X As Long, I As Long
Dim saHeader As SAFEARRAYHEADER
Dim hBa() As Byte 'store the string as byte array
ReDim nstr(Len(hstr) \ 2 - 1) As Byte
' table from characters "0123456789", "ABCDEF", "abcdef" to their numeric values (0 - 15)
If Table(49) = 0 Then
For I = 1 To 9: Table(48 + I) = I: Next
For I = 10 To 15: Table(55 + I) = I: Table(87 + I) = I: Next
End If
RedimArray byteArray, LenB(hstr), saHeader, StrPtr(hstr), VarPtrArray(hBa)
For X = 0 To LenB(hstr) - 3 Step 4
nstr(X \ 4) = Table(hBa(X)) * 16 + Table(hBa(X + 2)) 'bit shift and add
Next
DestroyArray VarPtrArray(hBa)
HexToAsc = StrConv(nstr, vbUnicode)
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.
Last edited by FireXtol; May 26th, 2010 at 01:47 AM.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|