Version 11 of binary scan

Updated 2005-12-17 01:42:09

binary scan string formatString ?varName varName ...?

The binary scan command parses fields from a binary string, returning the number of conversions performed. String gives the input to be parsed and formatString indicates how to parse it. Each varName gives the name of a variable; when a field is scanned from string the result is assigned to the corresponding variable.

As with binary format, the formatString consists of a sequence of zero or more field specifiers separated by zero or more spaces. Each field specifier is a single type character followed by an optional numeric count. Most field specifiers consume one argument to obtain the variable into which the scanned values should be placed. The type character specifies how the binary data is to be interpreted. The count typically indicates how many items of the specified type are taken from the data. If present, the count is a non-negative decimal integer or *, which normally indicates that all of the remaining items in the data are to be used. If there are not enough bytes left after the current cursor position to satisfy the current field specifier, then the corresponding variable is left untouched and binary scan returns immediately with the number of variables that were set. If there are not enough arguments for all of the fields in the format string that consume arguments, then an error is generated.

A similar example as with binary format should explain the relation between field specifiers and arguments in case of the binary scan subcommand:

 binary scan $bytes s3s first second

This command (provided the binary string in the variable bytes is long enough) assigns a list of three integers to the variable first and assigns a single value to the variable second. If bytes contains fewer than 8 bytes (i.e. four 2-byte integers), no assignment to second will be made, and if bytes contains fewer than 6 bytes (i.e. three 2-byte integers), no assignment to first will be made. Hence:

 puts [binary scan abcdefg s3s first second]
 puts $first
 puts $second

will print (assuming neither variable is set previously):

 1
 25185 25699 26213
 can't read "second": no such variable

It is important to note that the c, s, and S (and i and I on 64bit systems) will be scanned into long data size values. In doing this, values that have their high bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints), will be sign extended. Thus the following will occur:

 set signShort [binary format s1 0x8000]
 binary scan $signShort s1 val; # val == 0xFFFF8000

If you want to produce an unsigned value, then you can mask the return value to the desired size. For example, to produce an unsigned short value:

 set val [expr {$val & 0xFFFF}]; # val == 0x8000

Each type-count pair moves an imaginary cursor through the binary data, reading bytes from the current position. The cursor is initially at position 0 at the beginning of the data.

DMG, 2-Dec-03: It is also important to note that the scanning of float types is limited to the "endian" of the scanner. IEEE binary float to string conversion provides one way of converting them. Another way is to do a binary scan of the characters, binary format them in the proper order, and binary scan the now native order.


DMG, 2-Dec-03: Question: Does anyone know of a way/hack to scan in null terminated strings? I was somewhat surprised to see they were not part of the formatString set as they naturally fall into how Tcl works (well, how it used to). For example, I'm trying to read a file that has a 30-byte space allocated to hold 2 null-terminated strings.

19-Oct-2004: Try

 set null_term_string [lindex [split $string \000 ] 0]

sbron, 27-Sep-2005: I more frequently need unsigned results from [binary scan] than the default signed values. I created my own proc that adds a few new field specifiers that return unsigned values: C - unsigned byte, u - unsigned little-endian short, U - unsigned big-endian short, l - 32-bit unsigned little endian integer, and L - 32-bit unsigned big-endian integer.

 proc binscan {str fmtstr args} {
     # Create a format string using the built-in signed versions
     set format [string map {C c u s U S l i L I} $fmtstr]
     # Split the formatstring into the separate terms
     set i 0; set vars ""; set fmtlist ""
     foreach n [regexp -all -inline {[a-wA-W][0-9* ]*} $fmtstr] {
         lappend fmtlist $n
         lappend vars term([incr i])
     }

     # Execute the signed binary scan
     eval [linsert $vars 0 binary scan $str $format]
     #binary scan $str $format {expand}$vars

     # Define the mask values to apply to the special format specifiers
     array set mask {C 0xff u 0xffff U 0xffff l 0xffffffff L 0xffffffff}
     # Apply the mask and assign the results to the specified variables
     set i 0
     foreach n $fmtlist v $args {
         set type [string index $n 0]
         # Link to the variable in the calling stack frame
         upvar 1 $v var
         if {[info exists mask($type)]} {
             set list ""
             foreach t $term([incr i]) {
                 lappend list [expr {$t & $mask($type)}]
             }
             set var $list
         } else {
             set var $term([incr i])
         }
     }
 }

See also:


Tcl syntax help - Category Command - Category String Processing