Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
 
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, updated 2007-09-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C206 - C and C based languages - Character Strings
  [4633] String handling in C - new examples of extracting integers from a string - (2016-01-27)
  [4556] Strings in C - strncmp strncpy and friends - (2015-10-27)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [3718] Splitting a record into individual data values in C - (2012-05-04)
  [3593] Chars, char arrays and strings in C. Some early cautions and pitfalls. - (2012-01-26)
  [3146] Strings in C - (2011-01-25)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3122] When is a program complete? - (2011-01-06)
  [2844] Learning about Regular Expressions in C through examples - (2010-06-30)
  [2843] String functions in C - (2010-06-30)

T214 - Tcl/Tk - Other Facilities in Tcl
  [4762] Coverage map in Tcl - how many times has each proc been called? - (2017-09-28)
  [4525] What does Tcl do if you try to run a command that is not defined? - (2015-10-10)
  [4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
  [4207] Exception handling in Tcl - (2013-11-14)
  [3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
  [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
  [3287] Exceptions - Tcl style - (2011-05-12)
  [2467] Tcl - catching an error before your program crashes - (2009-10-22)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
  [782] Converting between Hex and Decimal in Tcl - (2006-06-28)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [461] Shortened interactive commands - (2005-10-11)
  [366] Error handling in Tcl through catch - (2005-07-02)
  [364] pu daily and p hourly - (2005-06-30)
  [239] What and why for the epoch - (2005-03-08)


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
Kasteel Elsloo - Michelin rated hotel.
4759 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, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1338_Han ... on-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat