CryptoSys API Library Manual

CNV_BytesFromHexStr

Decodes a hexadecimal-encoded string into an array of Bytes.

VB6/VBA Syntax

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)

Parameters

abData
[out] Byte array suitably dimensioned to receive output.
nDataLen
[in] Long specifying the length of the byte array.
strInput
[in] String of hexadecimal data to be decoded.

C/C++ Syntax

long _stdcall CNV_BytesFromHexStr(unsigned char *output, long out_len, const char *input);

Returns (VB6/C)

Long: If successful, the return value is the number of bytes in the decoded array; otherwise it returns a negative error code.

.NET Equivalent

Cnv.FromHex Method

COM/ASP Equivalent

conv.BytesFromHex
Public Function BytesFromHex(ByVal strHex As String) As Variant

See conv.BytesFromHex.

Remarks

Call the function with an empty array to find the required length.

Example

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

See Also

CNV_HexStrFromBytes CNV_HexFilter

[Contents] [Index]

[HOME]   [NEXT: CNV_HexFilter...]

Copyright © 2001-11 D.I. Management Services Pty Ltd. All rights reserved.