CryptoSys PKI Pro Manual

CNV_BytesFromB64Str

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

VBA/VB6 Syntax

Public Declare Function CNV_BytesFromB64Str Lib "diCrPKI.dll" (ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByVal strInput As String) As Long

nLen = CNV_BytesFromB64Str(lpOutput(0), nOutBytes, strInput)

C/C++ Syntax

long __stdcall CNV_BytesFromB64Str(unsigned char *lpOutput, long nOutBytes, const char *szInput);

Parameters

lpOutput
[out] array suitably dimensioned to receive output.
nOutBytes
[in] specifying the maximum number of bytes to be received.
szInput
[in] base64 data to be decoded.

Returns (VBA/C)

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

VBA Wrapper Syntax

Public Function cnvBytesFromB64Str (szB64 As String) As Byte()
Public Function cnvFromBase64 (strBase64 As String) As Byte()

.NET Equivalent

Cnv.FromBase64 Method

C++ (STL) Equivalent

static bvec_t dipki::Cnv::FromBase64 (const std::string &s)

Python Equivalent

static Cnv.frombase64(s)

Remarks

This uses the base64 encoding scheme from [RFC4648]. Pass a zero value for nOutBytes to find the required maximum possible number of bytes in the output array. The final array may be shorter.

@warning [Changed in v11.1] This function now returns an error if it finds an illegal character in the input string (previously any non-base64 character was just ignored). Whitespace characters (space, TAB, LF, CR, VT, FF) are still allowed and ignored but any other non-base64 characters will cause an error.

Example (VBA core function)

The following wrapper function will return the decoded bytes directly, with a default return value that won't cause a run-time error in the calling code.

Public Function cnvBytesFromB64Str(strB64 As String) As Byte()
' Returns byte array 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 = vbNullString
    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

New stricter behaviour in [v11.1]

Old behaviour in the VBA immediate window:

? cnvHexStrFromBytes(cnvBytesFromB64Str("---BEGIN---/ty6mHZUMhA="))
04418837FB72EA61D950C840

Result is 12 bytes long. The non-base64 character '-' is ignored. The valid but probably unintended characters "BEGIN" are decoded.

New behaviour:

? cnvHexStrFromBytes(cnvBytesFromB64Str("---BEGIN---/ty6mHZUMhA="))

? PKI_ErrorCode()
 8 

Result is 0 bytes long. Error code 8 (INVALID_DATA_ERROR) is set.

See Also

CNV_B64StrFromBytes CNV_B64Filter

[Contents] [Index]

[PREV: CNV_ByteEncoding...]   [Contents]   [Index]   
   [NEXT: CNV_BytesFromHexStr...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.