Base 64 Conversion (B64)
The base64 conversion code translates data to or from a format widely used for transmission over the internet.
The full format of this conversion code is
Used with OCONV() for output conversion, this operation converts data to base64 format. The data is returned as a continuous sequence of encoded characters. In common use, this data would then be divided into lines of manageable length, typically 72 characters. This can be achieved with the FOLD() function.
Used with ICONV() for input conversion, this operation converts base64 data back to its original form. Newlines and other filler characters are ignored.
Base 64 encoding is a byte level operation. On an ECS system, it would be necessary to transform data that may contain ECS characters to a byte oriented form. This might be, for example, an encoding such as UTF-8 or a simple byte string from the BS conversion code. The transformation applied will depend on how the data is being used.
OPENPATH '$HOLD' TO FVAR ELSE STOP 'Cannot open file'
MARK.MAPPING FVAR, OFF
READ TEXT FROM FVAR, 'REPORT.PDF' ELSE STOP 'Cannot read data'
MARK.MAPPING FVAR, ON
B64 = OCONV(TEXT, 'B64')
WRITE FOLD(B64, 72) TO FVAR, 'REPORT.B64'
The above program opens the $HOLD file. It then suppresses the normal directory file translation of newlines to field marks because the item to be read contains binary data (a PDF document). Having read this record, mark translation is re-enabled. The B64 conversion is used to translate the PDF to its base64 encoded equivalent. Finally, the FOLD() function is used to wrap the encoded data into 72 character lines. Because the target file is a directory file and mark translation has been re-enabled, the field marks in the data returned by the FOLD() function get replaced by newlines.