The main speed for my function relies on avoiding memory copy: ie. I could have simply done Char = Hex, but this would have made a new byte array which I did not want. So, instead I added two API calls to allow pointing Char to the existing Hex string. All the validation code makes the function a little slower. Despite this it is two times faster than HexToAsc.
If I remove all validation and space character handling (edit! includes space character handling):It gains a considerable amount of more speed: this works just like HexToAsc, but runs 3.5 times faster.Code:Public Function HexANSIToStringFast2(ByVal Hex As String) As String Dim Char() As Byte, Header(0 To 5) As Long, Ptr As Long Dim B As Byte, I As Long, J As Long, S As Long, TL(0 To 255) As Byte, TU(0 To 255) As Byte For B = 49 To 57: TL(B) = B - 48: TU(B) = TL(B) * 16: Next For B = 65 To 70: TL(B) = B - 55: TU(B) = TL(B) * 16: TL(B + 32) = TL(B): TU(B + 32) = TU(B): Next Header(0) = 1: Header(1) = 1: Header(3) = StrPtr(Hex): Header(4) = LenB(Hex) Ptr = ArrayPtr(Char): PutMem4 Ptr, VarPtr(Header(0)) If Char(4) <> 32 Then S = 4 Else S = 6 For I = 0 To Header(4) - 4 Step S Char(J) = TU(Char(I)) Or TL(Char(I + 2)) J = J + 1 Next I PutMem4 Ptr, 0 If J Then HexANSIToStringFast2 = StrConv(LeftB$(Hex, J), vbUnicode) Else Err.Raise 5 End If End Function
Update!
Project updated with fixed versions.




Reply With Quote