# Binary read/write hack with plain Tcl - (c) 1996 by Laurent Demailly #(for full binary support for tcl see http://www.lyot.obspm.fr/~dl/tclbin.html) # binary read byte (easy, empty 1 char string read == 0) proc readByte {chan} {set v 0;scan [read $chan 1] %c v;set v} # read a network order int proc readBinNetInt {chan} { expr ([readByte $chan]<<24)+([readByte $chan]<<16)+([readByte $chan]<<8)+\ [readByte $chan] } # write is more difficult, we need to copy the needed 0 from somewhere # the most portable hack I found is to have around a open channel like: if ![info exists _zerochan] { set _zerochan [open zero.tmp w+]; # create new channel seek $_zerochan 1; # seek to pos 1 puts -nonewline $_zerochan "d"; # write anything -> pos 0 now have a \0 ! seek $_zerochan 0; } # write a 0 byte: proc sendZero {chan} { global _zerochan; unsupported0 $_zerochan $chan 1; seek $_zerochan 0; } # write a network order binary int to an open stream proc writeBinNetInt {chan int} { for {set i 3} {$i>=0} {incr i -1} { set v [expr (($int)>>($i*8))&(0xff)]; if {$v} {puts -nonewline $chan [format %c $v]} {sendZero $chan} } }