Option Explicit
Private m_InitHex as Boolean
Private m_ByteToHex(0 To 255, 0 To 1) as Byte
Private m_HexToByte(48 To 70, 48 To 70) as Byte
Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination as Any, ByVal Length as Long, ByVal Fill as Byte)
Function HexToStr(HexText as String, Optional ByVal Separators as Long = 1) as String
Dim a as Long
Dim Pos as Long
Dim PosAdd as Long
Dim ByteSize as Long
Dim HexByte() as Byte
Dim ByteArray() as Byte
'Initialize the hex routine
If (Not m_InitHex) Then Call InitHex
'The destination string is half
'the size of the source string
'when the separators are removed
If (Len(HexText) = 2) Then
ByteSize = 1
Else
ByteSize = ((Len(HexText) + 1) \ (2 + Separators))
End If
ReDim ByteArray(0 To ByteSize - 1)
'Convert every HEX code to the
'equivalent ASCII character
PosAdd = 2 + Separators
HexByte() = StrConv(HexText, vbFromUnicode)
For a = 0 To (ByteSize - 1)
ByteArray(a) = m_HexToByte(HexByte(Pos), HexByte(Pos + 1))
Pos = Pos + PosAdd
Next
'Now finally convert the byte
'array to the return string
HexToStr = StrConv(ByteArray, vbUnicode)
End Function
Private Sub InitHex()
Dim a as Long
Dim b as Long
Dim HexBytes() as Byte
Dim HexString as String
'The routine is initialized
m_InitHex = True
'Create a string with all hex values
HexString = String$(512, "0")
For a = 1 To 255
Mid$(HexString, 1 + a * 2 + -(a < 16)) = Hex(a)
Next
HexBytes = StrConv(HexString, vbFromUnicode)
'Create the Str->Hex array
For a = 0 To 255
m_ByteToHex(a, 0) = HexBytes(a * 2)
m_ByteToHex(a, 1) = HexBytes(a * 2 + 1)
Next
'Create the Str->Hex array
For a = 0 To 255
m_HexToByte(m_ByteToHex(a, 0), m_ByteToHex(a, 1)) = a
Next
End Sub
Function StrToHex(text as String, Optional Separator as String = " ") as String
Dim a as Long
Dim Pos as Long
Dim Char as Byte
Dim PosAdd as Long
Dim ByteSize as Long
Dim ByteArray() as Byte
Dim ByteReturn() as Byte
Dim SeparatorLen as Long
Dim SeparatorChar as Byte
'Initialize the hex routine
If (Not m_InitHex) Then Call InitHex
'Initialize variables
SeparatorLen = Len(Separator)
'Create the destination bytearray, this
'will be converted to a string later
ByteSize = (Len(text) * 2 + (Len(text) - 1) * SeparatorLen)
ReDim ByteReturn(ByteSize - 1)
Call FillMemory(ByteReturn(0), ByteSize, Asc(Separator))
'We convert the source string into a
'byte array to speed this up a tad
ByteArray() = StrConv(text, vbFromUnicode)
'Now convert every character to
'it's equivalent HEX code
PosAdd = 2 + SeparatorLen
For a = 0 To (Len(text) - 1)
ByteReturn(Pos) = m_ByteToHex(ByteArray(a), 0)
ByteReturn(Pos + 1) = m_ByteToHex(ByteArray(a), 1)
Pos = Pos + PosAdd
Next
'Convert the bytearray to a string
StrToHex = StrConv(ByteReturn(), vbUnicode)
End Function