beetlebug▾
/

binary decode

Gives the bytes of a text in base64, hexadecimal, or uuencode.

binary decode base64 ?-strict? DATA
binary decode hex ?-strict? DATA
binary decode uuencode ?-strict? DATA

Description

Decodes the text DATA of one of the three forms of binary encode, the word after decode, and gives the bytes.

With no option, white space in DATA is ignored. For base64, the missing = at the end is no error, and a character that is not of base64 ends the bytes. For hex, both cases of the digits are valid, and a last digit with no pair is dropped. -strict makes white space and a character of no meaning an error.

The result is a byte string. Use encoding convertfrom to read it as text in an encoding.

Arguments

ArgumentTypeDefaultDescription
DATAstringrequiredThe text to decode.

Options

OptionTypeDefaultDescription
-strictflagnoneMakes white space and each character of no meaning an error.

-strict

Makes white space and each character of no meaning an error.

With -strict, each character of DATA must be of the form, so white space and a newline are errors too. The message names the character and its position from 0.

% binary decode base64 -strict aGVsbG8=
hello

% binary decode hex -strict 6869
hi
% binary decode base64 -strict "aGVs bG8="
invalid base64 character " " at position 4
-errorcode NONE

% binary decode base64 -strict a!
invalid base64 character "!" at position 1
-errorcode NONE

% binary decode hex -strict "68 65"
invalid hexadecimal digit " " at position 2
-errorcode NONE

Returns

A byte string.

Examples

% binary decode base64 aGVsbG8=
hello

# The missing = at the end is no error.
% binary decode base64 aGVsbG8
hello

% binary decode base64 "aGVs bG8="
hello

% binary decode hex 68656C6C6F
hello

% binary decode hex "68 65"
he

% binary decode uuencode [binary encode uuencode hello]
hello

% encoding convertfrom utf-8 [binary decode base64 w6k=]
é

Errors

% binary decode hex zz
invalid hexadecimal digit "z" at position 0
-errorcode NONE

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

% binary decode base64 -bogus 1 x
bad option "-bogus": must be -strict
-errorcode NONE

Differences from Tcl

Facts

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

See also