Group: comp.lang.tcl


Subject: correctly formatting binary data to 4byte int value
From: Glenn Jackman
Date: 11/13/2007 9:46:47 PM
At 2007-11-13 02:54PM, "yaipa" wrote: > data: > 00 00 00 00 00 E8 00 00 01 E8 00 00 01 E8 00 00 > > desired output > 0000E800E801E801 > > which I can do in Python using the following code snippet > > import struct, array > a = array.array('L') > > fh = open('cfg_data.bin', 'rb') > > # this reads in four bytes per array index > a.read(fh, 4) > > # format the output > s = "" > for i in a: > t = "%04X" % i > s = s + t > > # print the output > print s To simply translate your python: # read the data set fh [open cfg_data.bin r] fconfigure $fh -translation binary set data [read -nonewline $fh] close $fh # break it into a list (named "a") of 32-bit signed ints, little-endian binary scan $bindata i* a # format set s "" foreach i $a { append s [format %04X $i] } puts $s ;# prints "0000E800E801E801" as required I created the data file thusly: set data [join {00 00 00 00 00 E8 00 00 01 E8 00 00 01 E8 00 00} ""] set bindata [binary format H* $data] set f [open cfg_data.bin w] fconfigure $f -translation binary puts -nonewline $f $bindata close $f -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry