Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Handling Binary data in Tcl (with a note on C)
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 06:14:06)

 
Associated topics are indexed under
T214 - Tcl/Tk - Other Facilities in Tcl
C206 - C and C based languages - Character Strings

Back to
A series of tyre damages
Previous and next
or
Horse's mouth home
Forward to
Packages and Namespaces are like Suitcases and Aircraft

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 Aircraft
Handling Binary data in Tcl (with a note on C)
A series of tyre damages
Ignore case in Regular Expression
Expanding a grid - Tcl/Tk
Stable sorting - Tcl, Perl and others
Kasteel Elsloo - Michelin rated hotel.
1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho