binary scan
Reads values from a byte string into variables with a template of fields.
binary scan STRING FORMATSTRING ?VARNAME ...?
Description
Reads the bytes of STRING, one field of FORMATSTRING after the other, and puts the value of each field in the next VARNAME. The fields x, X, and @ move the cursor and use no variable. The result is the count of variables that got a value.
The letters are the letters of binary format, and a count has the same meaning. A field of text gives a string: the bytes for a, the bytes with no trailing spaces and zero bytes for A, and a string of digits for b, B, h, and H. A field of numbers with no count gives one number, and with a count or * a list of numbers.
An integer is signed. A u after the letter of an integer of 8, 16, or 32 bits, such as cu or Iu, reads it as unsigned.
When STRING has fewer bytes than a field needs, the scan stops there: that variable and the ones after it do not change. A * uses each byte that is left, and for a field of numbers only as many whole values as the bytes hold.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| STRING | byte string | required | The bytes to read. |
| FORMATSTRING | string | required | The template: fields, each a letter, an optional u, and an optional count. |
| VARNAME | variable name | none | A variable for the value of the next field that reads one. |
Returns
The count of variables that got a value.
Examples
% binary scan [binary format c3 {1 2 3}] c* l
1
% binary scan [binary format c3 {1 2 3}] c* l; set l
1 2 3
% binary scan \x01\x02 S v; set v
258
% binary scan \x01\x02 s v; set v
513
% binary scan \xff c v; set v
-1
% binary scan \xff cu v; set v
255
% binary scan \xff\xff\xff\xff iu v; set v
4294967295
% binary scan abc H* v; set v
616263
% binary scan \x81 B* v; set v
10000001
% binary scan "ab " A* v; set v
ab
% binary scan abc a1X1a1 x y; list $x $y
a a
% binary scan abc @2a1 v; set v
c
% binary scan \x01\x02\x03 c2c v w; list $v $w
{1 2} 3
# Three bytes hold one value of 16 bits.
% binary scan \x01\x02\x03 s* v; set v
513
# The field needs three bytes, so v does not change.
% set v old; list [binary scan ab a3 v] $v
0 old
% binary scan [binary format d 1.5] d v; set v
1.5
# Tcl 9.1 only
% binary scan "ab\0cd" C* v; set v
ab
Errors
% binary scan abc c
not enough arguments for all format specifiers
-errorcode NONE
% binary scan abc z v
bad field specifier "z"
-errorcode NONE
# Tcl 8.6 only
% binary scan "ab\0cd" C* v
bad field specifier "C"
-errorcode NONE
% binary scan abc
wrong # args: should be "binary scan value formatString ?varName ...?"
-errorcode TCL WRONGARGS
Tcl 8.6 and Tcl 9.1
- Tcl 9.1 has the field
C: the bytes up to the first zero byte of the field. In Tcl 8.6 it is a bad field specifier.
Differences from Tcl
wuandWuread a signed value. tclsh 8.6 reads an unsigned value, such as 9223372036854775809 in place of -9223372036854775807.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1