Binary Cheatsheet

I have been using Tcl for years, but I still need to lookup the manual every time I use binary format and binary scan. If you are like me, you will find this cheatsheet a handy reference.

  Endian    
Char Little/Big/Host Type Notes
a   byte string format pads with nulls
A   byte string format pads with spaces, scan strips trailing spaces/nulls
b b/B binary digits  
h h/H hex digits  
c   8 bit integer list In other words, ASCII chars
s s/S/t 16 bit integer list  
i i/I/n 32 bit integer list  
w w/W/m 64 bit integer list  
r r/R/f single prec. float list  
q q/Q/d double prec. float list  
x   null bytes count is number of bytes
X   move cursor back count is number of bytes to back up
@   move cursor to location count is byte position, zero based

Examples

set bin [binary format I2sH2a* {0x123 0x456} 17 ab "testing"]

# This returns 4 (number of conversions)
binary scan $bin I2sH2a* intlist intval hexval str

Steve Bennett (steveb@workware.net.au)


comments powered by Disqus