The IDL_BASE64 function uses MIME Base64 encoding to convert a byte array into a Base64 encoded scalar string, or to convert a Base64-encoded scalar string into a byte array.
The MIME Base64 encoding uses 64 characters, consisting of “A-Z”, “a-z”, “0-9”, “+”, and “/”. Every 3 bytes of the original array are converted into 4 characters. If the length of the final encoded string is not a multiple of 4, then it is padded with “=” characters. The 64 characters used in Base64 encoding were chosen because they are common to most character encodings, are printable, and are unlikely to be modified in transit through systems such as electronic mail.
This routine is written in the IDL language. Its source code can be found in the file idl_base64.pro in the lib subdirectory of the IDL distribution.
Examples
This example reads a JPEG image into a byte array, converts the array to a MIME Base64-encoded string, then converts the string back into a byte array and displays the two images side by side.
READ_JPEG, FILEPATH('rose.jpg', $
SUBDIRECTORY=['examples', 'data']), rose
HELP, rose
rs = SIZE(rose)
rose_dims = [rs[1], rs[2], rs[3]]
rose_encoded = IDL_BASE64(rose)
HELP, rose_encoded
PRINT, 'rose_encoded is ' + STRING(STRLEN(rose_encoded)) $
+' characters long'
rose2 = IDL_BASE64(rose_encoded)
HELP, rose2
rose3 = REFORM(rose2, rose_dims)
HELP, rose3
WINDOW, XSIZE=rose_dims[1]*2, YSIZE=rose_dims[2], $
TITLE='Original and Reconstituted images', /FREE
TV, rose, 0, /TRUE
TV, rose3, 1, /TRUE
This example converts an “ordinary” string into a Base64-encoded string:
orig_string = 'IDL is fun!'
orig_bytes = BYTE(orig_string)
HELP, orig_string
PRINT, STRLEN(orig_string)
HELP, orig_bytes
new_string = IDL_BASE64(orig_bytes)
PRINT, new_string
PRINT, STRING(IDL_BASE64(new_string))
Syntax
Result = IDL_BASE64(Input)
Return Value
If Input is a byte array, Result is a scalar string containing the Base64 encoded data.
If Input is a Base64 encoded string, Result is a byte vector containing the decoded data.
Arguments
Input
Input must be either an array of type byte or a Base64 encoded scalar string.
Keywords
None
Version History