CryptoSys API Library Manual

CNV_BytesFromB64Str

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

VB6/VBA Syntax

Public Declare Function CNV_BytesFromB64Str Lib "diCryptoSys.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strInput As String) As Long

nLen = CNV_BytesFromB64Str(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 base64 data to be decoded.

C/C++ Syntax

long _stdcall CNV_BytesFromB64Str(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

Use the in-built method System.Convert.FromBase64String or (safer) Cnv.FromBase64 Method

Remarks

Call the function with an empty array to find the required length. Non-base64 characters in the input string are ignored.

Example

Public Function cnvBytesFromB64Str(strB64 As String) As Variant
' Returns a Variant to an array of bytes decoded from a base64 string
    Dim abData() As Byte
    Dim nDataLen As Long
    
    ' Set default return value that won't cause a run-time error
    cnvBytesFromB64Str = StrConv("", vbFromUnicode)
    nDataLen = CNV_BytesFromB64Str(0, 0, strB64)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim abData(nDataLen - 1)
    nDataLen = CNV_BytesFromB64Str(abData(0), nDataLen, strB64)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim Preserve abData(nDataLen - 1)
    cnvBytesFromB64Str = abData
End Function
    Dim strBase64 As String
    Dim abData() As Byte
    Dim i As Integer
    strBase64 = "/ty6mHZUMhA="
    ' Convert base64 string to bytes
    abData = cnvBytesFromB64Str(strBase64)
    For i = LBound(abData) To UBound(abData)
        Debug.Print Hex(abData(i));
    Next
    Debug.Print

See Also

CNV_B64StrFromBytes CNV_B64Filter

[Contents] [Index]

[HOME]   [NEXT: CNV_BytesFromHexStr...]

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