Name

binary

binary options...

Convert data between Tcl string format and machine-dependent binary representation.

binary format formatString [args...]

Return a binary string in a format defined by formatString with data taken from args. The format string consists of zero or more field codes, each followed by an optional integer count. The field codes are listed here:

a

Chars (null padding)

A

Chars (space padding)

b

Binary (low-to-high)

B

Binary (high-to-low)

h

Hex (low-to-high)

H

Hex (high-to-low)

c

8-bit int

s

16-bit int (little-endian)

S

16-bit int (big-endian)

i

32-bit int (little-endian)

I

32-bit int (big-endian)

f

Float

d

Double

x

Nulls

X

Backspace

@

Absolute position

binary scan string formatString [varName...]

Parse a binary string according to the format defined in formatString and place the results in the specified variable names. Return the number of variables that were set. The format string is the same as for binary format except for the following:

a

Chars (no stripping)

A

Chars (stripping)

x

Skip forward

Example

set i 1234
    set j 3.14
    set s hello
    set str [binary format ida5 $i $j $s]
    binary scan $str ida5 i j s

Get Tcl/Tk in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.