Converts binary data into a Base64 character string, and returns a LONGCHAR containing the character data. The resulting LONGCHAR is in the code page specified by -cpinternal.

Syntax

BASE64-ENCODE ( expression )
expression
A MEMPTR or RAW expression containing the binary data you want to convert.
Note: The BASE64-ENCODE and BASE64-DECODE functions:
  • Use the Base64 Alphabet table as found in RFC 4648.
  • Do not enforce line lengths with separators.
  • Require use of the ‘=’ padding character.
  • Do not implement the Filename Safe Alphabet needed for Base64URL encoding.
Note: The BASE64-ENCODE, BASE64-DECODE, HEX-ENCODE, and HEX-DECODE functions do not do any byte ordering. However, the binary output of these functions may be a binary data type such as a MEMPTR or RAW. If the target of these operation’s output is another system where the order of bytes (endianness) differs, you must first normalize the data to what the receiving side is expecting. Otherwise, the results may not be as expected. For example, for a MEMPTR value that you are building yourself, you can use the SET-BYTE-ORDER statement to set the byte order for the MEMPTR variable before you read or write the value.

Example

The following example code uses the BASE64-ENCODE function:

DEFINE VARIABLE memValue    AS MEMPTR    NO-UNDO.
DEFINE VARIABLE counter     AS INTEGER   NO-UNDO.
DEFINE VARIABLE base64Value AS CHARACTER NO-UNDO.

/* Allocate 5 bytes and populate them with "Hello" (ASCII values) */
SET-SIZE(memValue) = 5.

PUT-BYTE(memValue, 1) = 72.  /* H */
PUT-BYTE(memValue, 2) = 101. /* e */
PUT-BYTE(memValue, 3) = 108. /* l */
PUT-BYTE(memValue, 4) = 108. /* l */
PUT-BYTE(memValue, 5) = 111. /* o */

/* Output the numeric byte values that will be encoded */
DO counter = 1 TO GET-SIZE(memValue):
  MESSAGE "Byte" counter ":" GET-BYTE(memValue, counter).
END.

/* Encode the MEMPTR as a Base64 character string */
base64Value = BASE64-ENCODE(memValue).
MESSAGE "BASE64-ENCODE result:" base64Value.

Running this code displays the numeric byte values stored in the MEMPTR, followed by a message showing the Base64‑encoded character string:

Byte 1: 72
Byte 2: 101
Byte 3: 108
Byte 4: 108
Byte 5: 111
BASE64-ENCODE result: SGVsbG8=

The following example shows encoded strings, with and without padding characters at the end of them:

DEFINE VARIABLE cleartext AS LONGCHAR NO-UNDO.
DEFINE VARIABLE encoded AS LONGCHAR NO-UNDO.
DEFINE VARIABLE rawdata AS MEMPTR NO-UNDO.

// when encoded this string will require padding characters.
cleartext = "needs padding".
COPY-LOB FROM cleartext TO rawdata CONVERT TARGET CODEPAGE "utf-8".
encoded =  BASE64-ENCODE(rawdata).
MESSAGE STRING(encoded) VIEW-AS ALERT-BOX.

// when encoded, this string does not have padding character at the end of it.
cleartext = ".no padding.".
COPY-LOB FROM cleartext TO rawdata CONVERT TARGET CODEPAGE "utf-8".
encoded =  BASE64-ENCODE(rawdata).
MESSAGE STRING(encoded) VIEW-AS ALERT-BOX.

Running this code produces the following output:

Padding: bmVIZHMgcGFkZGluZw==
No padding: Lm5vlHBhZGRpbmcu

See also

BASE64-DECODE function, SET-BYTE-ORDER statement