Base64Decode

See Also: Type Conversion Functions

Purpose

Base64Decode converts base64 data back to binary data.

Return Type

Pointer

Syntax

Function Base64Decode Global Pointer pBase64 Integer ByRef iBinaryLength Returns Pointer

What It Does

This converts a Base64 CString back to binary data. It is passed a pointer to the CString. It allocates memory for the binary data and returns the binary data via a pointer. The length of the data is returned by reference in iBinaryLength.

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