CryptoSys API Library Manual

CNV_B64StrFromBytes

Encodes an array of bytes into a base64-encoded string.

VB6/VBA Syntax

Public Declare Function CNV_B64StrFromBytes Lib "diCryptoSys.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByRef abData As Byte, ByVal nDataLen As Long) As Long

nRet = CNV_B64StrFromBytes(strOutput, nMaxChars, abData(0), nDataLen)

Parameters

strOutput
[out] String to receive encoded data.
nMaxChars
[in] Long specifying the maximum number of characters to be received.
abData
[in] Byte array of binary data to be encoded.
nDataLen
[in] Long number of bytes to be encoded.

C/C++ Syntax

long _stdcall CNV_B64StrFromBytes(char *szOutput, long nOutChars, const unsigned char *input, long nbytes);

Returns (VB6/C)

Long: If successful, the return value is the number of characters in the encoded string; otherwise it returns a negative error code.

.NET Equivalent

Use the in-built method System.Convert.ToBase64String or (safer) Cnv.ToBase64 Method (Byte[])

Remarks

The input array may contain bytes of any value. If vbNullString or an empty string ("") is specified for strOutput or zero for nMaxChars, the function will return the maximum possible size of the encoded string. The final result may be smaller. C/C++ programmers should add one to the returned length value when allocating memory.

Example

This wrapper function returns a base64-encoded string directly:

Public Function cnvB64StrFromBytes(abData() As Byte) As String
' Returns base64 string encoding of bytes in abData or empty string if error
    Dim strB64 As String
    Dim nB64Len As Long
    Dim nDataLen As Long
    
    On Error GoTo CatchEmptyData
    nDataLen = UBound(abData) - LBound(abData) + 1
    nB64Len = CNV_B64StrFromBytes(vbNullString, 0, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    strB64 = String$(nB64Len, " ")
    nB64Len = CNV_B64StrFromBytes(strB64, nB64Len, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    cnvB64StrFromBytes = Left$(strB64, nB64Len)
    
CatchEmptyData:

End Function

This example uses the function above:

    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
    strBase64 = ""
    ' Convert back to a base64 string
    strBase64 = cnvB64StrFromBytes(abData)
    Debug.Print strBase64

Producing the output:

FEDCBA9876543210
/ty6mHZUMhA=

See Also

CNV_BytesFromB64Str CNV_B64Filter

[Contents] [Index]

[HOME]   [NEXT: CNV_BytesFromB64Str...]

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