#!/usr/local/bin/tclsh package provide binary_read 0.9 package require Tcl 8.2 namespace eval binary_read { proc usage {} { return "binary_read:\nAll procs take a filehandle, read a number from the filehandle, and return it.\nThe types are: \n\tlittle_double_fp\tLittle-endian double\n\tbig_double_fp\tBig-endian double\n\tbig_int\tBig-endian integer\n\tlittle_int\tLittle-endian integer\n\tlittle_int_16\tLittle-endian 2-byte integer\n\tbig_int_16\tBig-endian 2-byte integer\n\tbin_num\tLittle-endian 1-byte integer" } proc little_double_fp {filehandle} { if {$::tcl_platform(byteOrder) == "bigEndian"} { set xm "" for {set i 1} {$i <= 8} {incr i} { set xm [read $filehandle 1]$xm } } else { set xm [read $filehandle 8] } binary scan $xm d* X return $X } proc big_double_fp {filehandle} { if {$::tcl_platform(byteOrder) != "bigEndian"} { set xm "" for {set i 1} {$i <= 8} {incr i} { set xm [read $filehandle 1]$xm } } else { set xm [read $filehandle 8] } binary scan $xm d* X return $X } proc big_int {filehandle} { set x [read $filehandle 4] binary scan $x I* X return $X } proc little_int {filehandle} { set x [read $filehandle 4] binary scan $x i* X return $X } proc little_int_16 {filehandle} { set x [read $filehandle 2] binary scan $x s* X return $X } proc big_int_16 {filehandle} { set x [read $filehandle 2] binary scan $x S* X return $X } proc bin_num {filehandle} { set sum 0 set x [read $filehandle 1] binary scan $x b* X for {set i 0} {$i < [string length $X]} {incr i} { if {[string index $X $i]} { incr sum [expr int(pow(2,$i))] } } return $sum } }