StrConv in itself isn't slow... but you can limit the amount of calls into it. Simply remove the first StrConv and adjust the rest of your calculation to match it -> a lot more speed. Also Dim hBv(1) As Byte doesn't need to be an array: if you use two variables instead you again get more speed.
Updated!
This function now fully validates given input. It also allows any invalid character from range 0 to 127 to work as a separator that gets ignored. The formatting can be anything as long as two consecutive characters make up a valid hex pair.
Just to show what you can do:Code:Option Explicit Private Declare Function ArrayPtr Lib "msvbvm60" Alias "VarPtr" (Arr() As Any) As Long Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As Long, ByVal Value As Long) Public Function HexANSIToStringFast3(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, TL(0 To 255) As Byte, TU(0 To 255) As Byte, TI(0 To 255) As Byte For I = 0 To 127: TI(I) = 1: Next I For I = I To 255: TI(I) = 2: Next I For B = 48 To 57: TL(B) = B - 48: TU(B) = TL(B) * 16: TI(B) = 0: 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): TI(B) = 0: TI(B + 32) = 0: Next Header(0) = 1: Header(1) = 1: Header(3) = StrPtr(Hex): Header(4) = LenB(Hex) Ptr = ArrayPtr(Char): PutMem4 Ptr, VarPtr(Header(0)) For I = 0 To Header(4) - 4 Step 4 B = TI(Char(I)) If B = 0 Then Char(J) = TU(Char(I)) Or TL(Char(I + 2)) J = J + 1 ElseIf B = 1 Then I = I - 2 Else J = 0 Exit For End If Next I PutMem4 Ptr, 0 If J Then HexANSIToStringFast3 = StrConv(LeftB$(Hex, J), vbUnicode) Else Err.Raise 5 End If End Function
Debug.Print HexANSIToStringFast3("[48] [45] [6c] - [6C] [6f]")




Reply With Quote