In Tcl, all variables are held as strings, and most of the commands will split / divide strings at new line or space characters by default. However, there are a few commands that do NOT make that distinction and since a Tcl string (Unlike a C string) may contain any bit pattern at all, they provide a very useful tool for binary data handling. Here they are:
read. Read in a certain number of bytes from a file handle (up to agiven maximum or to the end of file, irrespective of the characters read).
Example:
set header [read $stuff 10]
binary scan. Divide a string into a series of separate variables, using a format string that's given as a parameter.
Example:
binary scan $header a3a3ss type version ecs why
Takes 3 chars from $header into the variable called type, the next three into the variable called version, the next two (as a 16 bit integer) into ecs and the next two as another 16 bit integer into why. Formats can include 16 and 32 bit, big and little endian, etc
binary format. Take a series of values and save them into a single string. This is the opposite of
binary scan in many ways, but there are a few more differenced in the format, the inputs are given with $s (as you would expect) and the output is returned.
Example:
binary format ss $hilda $stan
Returns a 4 character string having packed in the two decimal numbers in the Stan and Hilda variables as 16 bit integers
puts. Puts will output any string; remember to use the
-nonewline option if you don't want to an extra c/r added. Then remember than you might need
flush or
fconfigure.
Putting it all together - code that reads the start of a .gif file and tells the user how tall and wide a clickable image in a web page would be, assuming a default 1 pixel border:
set stuff [open tongue.gif r]
set header [read $stuff 10]
binary scan $header a3a3ss type version ecs why
close $stuff
incr ecs 2
incr why 2
puts "It is a $type file version $version size $ecs by $why"
puts -nonewline "(Size allows 2 extra pixels for a clickable "
puts "border in this demo"
Fill code with comments:
Here
Note on Strings in C. In C, you may store any bit pattern that you like in
An array oc Chars. However, if you use the built in
string handlers to manipulate that array of chars, you'll find that they all assume a null character (\0 or 0x00) as the end of string and will truncate at that point, or overrun if you char array doesn't contain a null.
(written 2007-09-09, updated 2007-09-11)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T214 - Tcl/Tk - Other Facilities in Tcl [239] What and why for the epoch - (2005-03-08)
[364] pu daily and p hourly - (2005-06-30)
[366] Error handling in Tcl through catch - (2005-07-02)
[461] Shortened interactive commands - (2005-10-11)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[3287] Exceptions - Tcl style - (2011-05-12)
[3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
[3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
[4207] Exception handling in Tcl - (2013-11-14)
[4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
[4525] What does Tcl do if you try to run a command that is not defined? - (2015-10-10)
[4762] Coverage map in Tcl - how many times has each proc been called? - (2017-09-28)
C206 - C and C based languages - Character Strings [2843] String functions in C - (2010-06-30)
[2844] Learning about Regular Expressions in C through examples - (2010-06-30)
[3122] When is a program complete? - (2011-01-06)
[3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
[3146] Strings in C - (2011-01-25)
[3593] Chars, char arrays and strings in C. Some early cautions and pitfalls. - (2012-01-26)
[3718] Splitting a record into individual data values in C - (2012-05-04)
[4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
[4556] Strings in C - strncmp strncpy and friends - (2015-10-27)
[4633] String handling in C - new examples of extracting integers from a string - (2016-01-27)
Some other Articles
Google, wwmdirectory, Freshwater, ATP - new scam?Moving to Melksham? Househunting map.Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python)Packages and Namespaces are like Suitcases and AircraftHandling Binary data in Tcl (with a note on C)A series of tyre damagesIgnore case in Regular ExpressionExpanding a grid - Tcl/TkKasteel Elsloo - Michelin rated hotel.