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
| Argument | Type | Default | Description | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| FORMATSTRING | string | required | The template: fields, each a letter and an optional count.
| ||||||||||||||||||||||||||||||||
| ARG | value | none | A 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
X*keeps only the bytes before the cursor, and tclsh 8.6 keeps each byte and moves the cursor to the start.@*is an error, and tclsh 8.6 moves to the end.x*gives one zero byte, and in tclsh 8.6 it is an error.- An integer that is too large for a field of 32 bits or less keeps its low bits. tclsh 8.6 gives the error
integer value too large to representfor a value such as 4294967296 ini.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1