From m204wiki
Jump to navigation Jump to search

Convert byte string to base64

Note: Many $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $Base64_Encode function is the StringToBase64 function.

This function converts a byte string into its base64 encoding. It is identical to $Lstr_Base64_Encode with the (hopefully obvious) exception that it is not longstring capable. As such, $Lstr_Base64_Encode is recommended over $Base64_Encode as it is a more flexible version of the latter.

The $Base64_Encode function accepts one argument and returns a string result which is the base64 encoding of that argument.


%coded = $Base64_Encode(string)

  • %coded is a string result set to the base64 encoding of string.
  • %string is a byte string of length 189 or less. If this argument is longer than 189 bytes, the request is canceled.

Usage notes

  • Because the maximum length of the argument is 189 bytes, if you have a longer stream to encode, you can do it in pieces, where any piece is a multiple of 3 bytes long (3 bytes are evenly packed into 4 bytes of a base64 encoding). For example, if you have a byte string which is the concatenation of items in a Sirius $list, and you want to print the base64 encoding of that string, possibly producing multiple lines of length 80, you can use the following approach:

    %S = FOR %I FROM 1 TO $ListCnt(%L) %T = $ListInf(%L, %I) REPEAT WHILE $LEN(%S) + $LEN(%T) > 60 %X = 60 - $LEN(%S) %U = %S WITH $SUBSTR(%T, 1, %X) PRINT $Base64_Encode(%U) %T = $SUBSTR(%T, %X + 1) %S = END REPEAT %S = %S WITH %T END FOR PRINT $Base64_Encode(%S)

  • As of Sirius Mods Version 6.8, you can do base64 encoding of strings longer than 255 bytes using $Lstr_Base64_Encode, but if, as in the example above, the intent is to break the result into smaller chunks, anyway, there probably wouldn't be a marked difference between using $Base64_Decode or its longstring equivalent.
  • $Base64_Decode is the inverse of $Base64_Encode.


Given this argument of length 3:

%junk = $Base64_Encode($X2C('001083'))

%junk would be set to the byte string (of length 4) represented in character as ABCD.

Products authorizing $Base64_Encode