Decodes a hexadecimal-encoded string into an array of Bytes.
Public Declare Function CNV_BytesFromHexStr Lib "diCryptoSys.dll"
(ByRef abData As Byte, ByVal nDataLen As Long, ByVal strInput As String) As Long
nRet = CNV_BytesFromHexStr(abData(0), nDataLen, strInput)
Byte array suitably dimensioned to receive output.Long specifying the length of the byte array.String of hexadecimal data to be decoded.
long _stdcall CNV_BytesFromHexStr(unsigned char *output, long out_len, const char *input);
Long: If successful, the return value is the number of bytes in the decoded array;
otherwise it returns a negative error code.
conv.BytesFromHex
Public Function BytesFromHex(ByVal strHex As String) As Variant
See conv.BytesFromHex.
Call the function with an empty array to find the required length.
Wrapper function to return byte-array (as a Variant) directly:-
Public Function cnvBytesFromHexStr(strHex As String) As Variant ' Returns a Variant to an array of bytes decoded from a hex string Dim abData() As Byte Dim nDataLen As Long ' Set default return value that won't cause a run-time error cnvBytesFromHexStr = StrConv("", vbFromUnicode) nDataLen = CNV_BytesFromHexStr(0, 0, strHex) If nDataLen <= 0 Then Exit Function End If ReDim abData(nDataLen - 1) nDataLen = CNV_BytesFromHexStr(abData(0), nDataLen, strHex) If nDataLen <= 0 Then Exit Function End If ReDim Preserve abData(nDataLen - 1) cnvBytesFromHexStr = abData End Function
Example of use:
Dim strHex As String
Dim abData() As Byte
Dim i As Integer
strHex = "FEDCAB9876543210"
' Convert hex string to bytes
abData = cnvBytesFromHexStr(strHex)
Debug.Print "{";
For i = LBound(abData) To UBound(abData)
Debug.Print Hex(abData(i)) & ", ";
Next
Debug.Print "}"
strHex = ""
' Convert back to a hex string
strHex = cnvHexStrFromBytes(abData)
Debug.Print strHex
Output:
{FE, DC, AB, 98, 76, 54, 32, 10, }
FEDCAB9876543210
CNV_HexStrFromBytes
CNV_HexFilter