Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

5-byte binary

Status
Not open for further replies.

Bong

Programmer
Dec 22, 1999
2,063
US
I had occasion to write a 5-byte number to a binary file. The only way I could think of to do it was to "format" the number into a 64-bit (W) number, "scan" it into a HEX string, parse the string 1 byte at a time, and write out the 5 individual (least significant) bytes:
Code:
     set t4 [binary format W $newt]
     binary scan $t4 H16 hxt
     for {set i 6} {$i<16} {incr i 2} {
         set i2 [expr {$i+1}]
         set bt [string range $hxt $i $i2]
         scan $bt %x bt2
         puts -nonewline $fid [binary format c $bt2]
     }
Is there a better way?

Bob Rashkin
rrashkin@csc.com
 
Why not write the number by one-byte chunk, converting each byte to ascii?

HTH

ulis
 
Ulis,
Sorry to be dense but I don't understand how that's different from what I am doing now. Are you suggesting a way to cut out some of the steps? If so, I don't understand?

Bob Rashkin
rrashkin@csc.com
 
Sorry, Bong, I misunderstood the beginning (I thought you had ascii strings and wanted to transform them in 5 bytes integers).
I'ld do exactly what you did.

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top