beetlebug▾
/

binary format

Packs values into a byte string with a template of fields.

binary format FORMATSTRING ?ARG ...?

Description

Makes a byte string from the ARG values, one field of FORMATSTRING after the other. Each field uses the next ARG, except x, X, and @, which use none.

For the fields of text (a, A, b, B, h, and H), the count is the count of bytes, bits, or hexadecimal digits to use from the ARG, and * uses all of it. A value shorter than the count is filled: a with zero bytes, A with spaces, and the others with zero bits. With no count the field uses one unit. A character of the ARG of a or A at U+0100 or above gives the low 8 bits of its code.

For the fields of numbers, the ARG of a field with no count is one number. With a count, the ARG is a list, and the field uses that count of its elements, or each element with *. A list with fewer elements than the count is an error, and the elements after the count are ignored. An integer keeps the low bits of its size, so 256 in a field of 8 bits gives a zero byte.

The result is a byte string: use binary encode hex to see its bytes.

Arguments

ArgumentTypeDefaultDescription
FORMATSTRINGstringrequiredThe template: fields, each a letter and an optional count.
ValueDescription
aBytes of a string, filled with zero bytes.
ABytes of a string, filled with spaces.
bBits from a string of 0 and 1, the low bit of each byte first.
BBits from a string of 0 and 1, the high bit of each byte first.
hHexadecimal digits, the low half of each byte first.
HHexadecimal digits, the high half of each byte first.
cAn integer of 8 bits.
sAn integer of 16 bits, little endian. S is big endian, and t is the order of the host.
iAn integer of 32 bits, little endian. I is big endian, and n is the order of the host.
wAn integer of 64 bits, little endian. W is big endian, and m is the order of the host.
fA float of 32 bits in the order of the host. r is little endian, and R is big endian.
dA float of 64 bits in the order of the host. q is little endian, and Q is big endian.
xA zero byte for each count. It uses no ARG.
XMoves the cursor back by the count of bytes. It uses no ARG.
@Moves the cursor to the byte of the count, from 0, and fills with zero bytes up to it. It uses no ARG.
ARGvaluenoneA value for the next field that uses one: a string, a number, or a list of numbers.

Returns

A byte string.

Examples

% binary encode hex [binary format S 4660]
1234

% binary encode hex [binary format s 4660]
3412

% binary encode hex [binary format I 305419896]
12345678

% binary encode hex [binary format i 305419896]
78563412

% binary encode hex [binary format W 0x1122334455667788]
1122334455667788

% binary encode hex [binary format c3 {17 34 51}]
112233

% binary encode hex [binary format c* {17 34 51}]
112233

# The elements after the count are ignored.
% binary encode hex [binary format c2 {17 34 51}]
1122

% binary encode hex [binary format a3 ab]
616200

% binary encode hex [binary format A3 ab]
616220

% binary encode hex [binary format H* abc]
abc0

% binary encode hex [binary format B8 10000001]
81

% binary encode hex [binary format R 1.5]
3fc00000

% binary encode hex [binary format Q 1.5]
3ff8000000000000

% binary encode hex [binary format a1x2a1 a b]
61000062

% binary encode hex [binary format a2@4 ab]
61620000

% binary encode hex [binary format a2X1a1 ab c]
6163

# White space between fields is ignored.
% binary encode hex [binary format "c c" 17 34]
1122

# 272 is 0x110, and c keeps the low 8 bits.
% binary encode hex [binary format c 272]
10

% binary encode hex [binary format a* é]
e9

Errors

% binary format c
not enough arguments for all format specifiers
-errorcode NONE

% binary format c4 {1 2 3}
number of elements in list does not match count
-errorcode NONE

% binary format c abc
expected integer but got "abc"
-errorcode TCL VALUE NUMBER

% binary format f abc
expected floating-point number but got "abc"
-errorcode NONE

% binary format H* xyz
expected hexadecimal string but got "xyz" instead
-errorcode NONE

% binary format B* 102
expected binary string but got "102" instead
-errorcode NONE

% binary format z 1
bad field specifier "z"
-errorcode NONE

% binary format
wrong # args: should be "binary format formatString ?arg ...?"
-errorcode TCL WRONGARGS

Differences from Tcl

Facts

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

See also