beetlebug▾
/

binary encode

Gives the bytes of a byte string as text: base64, hexadecimal, or uuencode.

binary encode base64 ?-maxlen LENGTH? ?-wrapchar CHAR? DATA
binary encode hex DATA
binary encode uuencode ?-maxlen LENGTH? ?-wrapchar CHAR? DATA

Description

Encodes the bytes of DATA as text in one of three forms, the word after encode:

-maxlen and -wrapchar set the lines of base64 and uuencode. hex has no options. A character of DATA at U+0100 or above stands for the low 8 bits of its code, so encode text with encoding convertto first.

Arguments

ArgumentTypeDefaultDescription
DATAbyte stringrequiredThe bytes to encode.

Options

OptionTypeDefaultDescription
-maxleninteger, 0 or more0 for base64, 61 for uuencodeBreaks the text into lines of at most LENGTH characters.
-wrapcharstringa newlineSets the text between two lines.

-maxlen LENGTH

Breaks the text into lines of at most LENGTH characters.

With a LENGTH above 0, -wrapchar goes after each LENGTH characters of base64 text. A LENGTH of 0 gives one line. For uuencode, LENGTH counts each character of a line with its length character, and a LENGTH below 5 is an error.

# A line of 9 characters holds 6 bytes.
% binary encode uuencode -maxlen 9 xxxxxxxxxx
&>'AX>'AX
$>'AX>`


% binary encode base64 -maxlen 4 hello
aGVs
bG8=

% binary encode base64 -maxlen 0 hello
aGVsbG8=
% binary encode base64 -maxlen x x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

% binary encode base64 -maxlen -1 x
line length out of range
-errorcode NONE

% binary encode uuencode -maxlen 1 x
line length out of range
-errorcode NONE

-wrapchar CHAR

Sets the text between two lines.

CHAR goes between the lines of -maxlen, and for uuencode at the end of each line. An empty CHAR joins the lines.

% binary encode base64 -maxlen 4 -wrapchar | hello
aGVs|bG8=

% binary encode base64 -wrapchar "" -maxlen 4 hello
aGVsbG8=

% binary encode uuencode -wrapchar | hello
%:&5L;&\|
% binary encode base64 -bogus 1 x
bad option "-bogus": must be -maxlen or -wrapchar
-errorcode NONE

Returns

The text.

Examples

% binary encode base64 hello
aGVsbG8=

% binary encode hex hello
68656c6c6f

% binary encode uuencode hello
%:&5L;&\


% binary encode base64 [encoding convertto utf-8 é]
w6k=

% binary encode base64 ""

Errors

% binary encode bogus x
unknown or ambiguous subcommand "bogus": must be base64, hex, or uuencode
-errorcode NONE

% binary encode hex -maxlen 2 x
wrong # args: should be "binary encode hex data"
-errorcode NONE

% binary encode base64
wrong # args: should be "binary encode base64 ?-maxlen len? ?-wrapchar char? data"
-errorcode NONE

% binary encode
wrong # args: should be "binary encode|decode format ?-option value ...? data"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also