Version 8 of binary

Updated 2003-10-15 09:09:27

binary - Insert and extract fields from binary strings

(Dissect and join binary data into/from tcl values)

binary format formatString ?arg arg ...?

binary scan string formatString ?varName varName ...?

http://purl.org/tcl/home/man/tcl8.4/TclCmd/binary.htm


This is the primary command to use when Working with binary data.

For dealing with binary data on C level see also http://www.tcl.tk/man/tcl8.4/TclLib/ByteArrObj.htm

See Binary representation of numbers for an example of usage.

What are the issues when dealing with special values - 64 bits for instance?


See also:


Examples

In a conversation in the Tcl'ers Chat about how to display the bits in a number DKF came up with this:

 binary scan [binary format I $value] B32 x; set x

GPS: I use the following string to/from hex conversion procedures to store files that may contain special characters that intefere with the OS filesystem in my Grindweb program:

 proc convert.string.to.hex str {
  binary scan $str H* hex 
  return $hex
 }

 proc convert.hex.to.string hex {
  foreach c [split $hex ""] { 
   if {![string is xdigit $c]} {
    return "#invalid $hex"
   }
  }
  binary format H* $hex
 }

Tcl syntax help - Category Command