$Base64 Encode: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
(Automatically generated page update)
Line 12: Line 12:
The returned value is the base 64 encoding of the argument string.
The returned value is the base 64 encoding of the argument string.
==Syntax==
==Syntax==
<p class="syntax"><section begin="syntax" />%CODED = $Base64_Encode(string)
<p class="syntax">%CODED = $Base64_Encode(string)
<section end="syntax" /></p>
</p>
<p>
<p>
</p>
</p>

Revision as of 18:16, 10 April 2013

Convert byte string to base 64

Most Sirius $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $Base64_Encode function is the StringToBase64 (String function).

This function converts a byte string into its base 64 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 base 64 encoding of that argument.

The first argument is a byte string of length 189 or less. If this argument is longer than 189 bytes, the request is canceled.

The returned value is the base 64 encoding of the argument string.

Syntax

%CODED = $Base64_Encode(string)

%CODED is set to the base 64 encoding of string.


For example, given the 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'.

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 base 64 encoding). For example, if you have a byte string which is the concatenation of items in a Sirius $list and you wish to print the base 64 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 base 64 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.

This $function is new in Version 6.0 of the Sirius Mods.

Products authorizing $Base64_Encode

64_Encode