encode

The encode statement is used to base16 or base64 encode the contents of a variable or a named buffer.

Syntax

encode base16|base64varName|{buffer_name}

Details

The encode statement will encode the contents of an existing variable or named buffer, replacing those contents with the encoded version.

The result of encoding the contents will increase their length. With base16 encoding the new length will be double the original. With base64 encoding the new length will be greater than the original but the exact size increase will depend on the contents being encoded.

When encoding a variable, if the size of the result after encoding exceeds the maximum allowable length for a variable value (8095 characters) then the USE script will fail and an error will be returned.

Encoding an empty variable or buffer will produce an empty result

Example

The following script ...

var testdata = "Text to be encoded"

print Encoding a variable ...
# Base16-encode a variable
var encode_me = ${testdata}
encode base16 encode_me
print Encoded base16 result is: ${encode_me}

# Base64-encode a variable
var encode_me = ${testdata}
encode base64 encode_me
print Encoded base64 result is: ${encode_me}

print Encoding a buffer ...
# Base16-encode a buffer
buffer encode_buf = data ${testdata}
encode base16 {encode_buf}
print Encoded base16 result is: {encode_buf}

# Base64-encode a buffer
buffer encode_buf = data ${testdata}
encode base64 {encode_buf}
print Encoded base64 result is: {encode_buf}

... produces the following output:

Encoding a variable ...
Encoded base16 result is: 5465787420746F20626520656E636F646564
Encoded base64 result is: VGV4dCB0byBiZSBlbmNvZGVk
Encoding a buffer ...
Encoded base16 result is: 5465787420746F20626520656E636F646564
Encoded base64 result is: VGV4dCB0byBiZSBlbmNvZGVk

Last updated