Base64Encode

See Also: Type Conversion Functions

Purpose

Base64Encode converts binary data to base64 data.

Return Type

Pointer

Syntax

Function Base64Encode Global Pointer pBinaryData Integer iBinaryLength Returns Pointer

What It Does

This converts binary data to Base64. It is passed a pointer to the binary data and it’s length. It allocates and creates a memory based CString that contains the encoded data and returns it as a pointer. The length of this string is obtained by calling CStringLength().

You must release the memory with Free().

This works at the memory level, which means it can be used without worrying about string size limitations. If these size limitations are not an issue, they can be easily used with strings as well.

If your binary data and encoded data (which will be 33% larger than the binary data) fit within the maximum string length, you could pass the data in and out as strings as follows. Using these methods hides the need to allocate and free memory. This also shows how to use the global Base64Encode and Base64Decode functions.

DataFlex allows for string to contain embedded zero bytes, which makes it safe to pass and return binary data, which may contain zero bytes, as strings. However, this is a fragile technique and there are places in the runtime where an embedded zero will cause a string to be truncated. Therefore when working with binary data, it is advised that you avoid the use of strings (use UChar[] via Base64DecodeUCharArray instead). The example below could be used if the data to be encoded was already a string.

Function Base64EncodeString String sText Returns String

    Pointer pBase64

    String sResult

    Integer iVoid

 

    Move (Base64Encode(AddressOf(sText), SizeOfString(sText))) to pBase64

    Move (PointerToString(pBase64)) to sResult

    Move (Free(pBase64)) to iVoid

 

    Function_Return sResult

End_Function

 

Function Base64DecodeString String sBase64 Returns String

    Pointer pBinary

    String sBinary

    Integer iVoid iLen

 

    Move (Base64Decode(AddressOf(sBase64), &iLen)) to pBinary

    Move (Repeat(Character(0), iLen)) to sBinary

    Move (MemCopy(AddressOf(sBinary), pBinary, iLen)) to iVoid

    Move (Free(pBinary)) to iVoid

    

    Function_Return sBinary

End_Function