binary(3tcl)
π NAME π
binary β insert and extract fields from binary strings. bestie, this is the page for making bytes behave. π¦
So you're reading binary(3tcl) from the Tcl Built-In Commands manual. Hi. Welcome. Sit down. π
β¨ SYNOPSIS β¨
binary decode format ?-option value ...? data
binary encode format ?-option value ...? data
binary format formatString ?arg arg ...?
binary scan string formatString ?varName varName ...?
Four subcommands, four flavors, all of them iconic in their own way. periodt. π
π DESCRIPTION π
okay so, this command is your whole toolkit for manipulating binary data. no cap.
The subcommand binary format creates a binary string from normal Tcl values. For example, given the values 16 and 22, on a 32-bit architecture, it might produce an 8-byte binary string consisting of two 4-byte integers, one for each of the numbers. Literally just vibes going in, bytes coming out. π§
The subcommand binary scan does the opposite: it extracts data from a binary string and returns it as ordinary Tcl string values. The glow-up is real.
The binary encode and binary decode subcommands convert binary data to or from string encodings such as base64 (used in MIME messages for example). MIME girlies know. π
Note that other operations on binary data, such as taking a subsequence of it, getting its length, or reinterpreting it as a string in some encoding, are done by other Tcl commands β respectivelystring range,string lengthandencoding convertfromin the example cases. So don't come forbinaryfor stuff it never claimed. π
A binary string in Tcl is merely one where all the characters it contains are in the range \u0000-\u00FF. that's it. that's the whole definition. π
π₯ BINARY ENCODE AND DECODE π₯
When encoding binary data as a readable string, the starting binary data is passed to the binary encode command, together with the name of the encoding to use and any encoding-specific options desired. Data which has been encoded can be converted back to binary form using binary decode. The binary encode command raises an error if the data argument is not binary data.
The following formats and options are supported. (yes, all of them. we do not skip.) π
base64 π
The base64 binary encoding is commonly used in mail messages and XML documents, and uses mostly upper and lower case letters and digits. It has the distinction of being able to be rewrapped arbitrarily without losing information. lowkey the most forgiving encoding out there. πΈ
During encoding, the following options are supported:
-maxlen lengthβ Indicates that the output should be split into lines of no more than length characters. By default, lines are not split. one long iconic line, no breaks. β¨-wrapchar characterβ Indicates that, when lines are split because of the-maxlenoption, character should be used to separate lines. By default, this is a newline character,\n.
During decoding, the following options are supported:
-strictβ Instructs the decoder to throw an error if it encounters any characters that are not strictly part of the encoding itself. Otherwise it ignores them. RFC 2045 calls for base64 decoders to be non-strict. so by default she's chill, but you can make her strict if you want. π
hex π§
The hex binary encoding converts each byte to a pair of hexadecimal digits that represent the byte value as a hexadecimal integer. When encoding, lower characters are used. When decoding, upper and lower characters are accepted. doesn't play favorites on the way in.
No options are supported during encoding. During decoding, the following options are supported:
-strictβ Instructs the decoder to throw an error if it encounters whitespace characters. Otherwise it ignores them. spaces? gone. unless you asked her to care. π
uuencode π¦
The uuencode binary encoding used to be common for transfer of data between Unix systems and on USENET, but is less common these days, having been largely superseded by the base64 binary encoding. it's giving vintage. it's giving heritage piece. still fully supported though, bestie. π
During encoding, the following options are supported (though changing them may produce files that other implementations of decoders cannot process):
-maxlen lengthβ Indicates the maximum number of characters to produce for each encoded line. The valid range is 5 to 85. Line lengths outside that range cannot be accommodated by the encoding format. The default value is 61.-wrapchar characterβ Indicates the character(s) to use to mark the end of each encoded line. Acceptable values are a sequence of zero or more characters from the set{ \x09 (TAB), \x0B (VT), \x0C (FF), \x0D (CR) }followed by zero or one newline\x0A(LF). Any other values are rejected because they would generate encoded text that could not be decoded. The default value is a single newline.
During decoding, the following options are supported:
-strictβ Instructs the decoder to throw an error if it encounters anything outside of the standard encoding format. Without this option, the decoder tolerates some deviations, mostly to forgive reflows of lines between the encoder and decoder. she's being nice on purpose. that's the whole feature. π«Ά
Note that neither the encoder nor the decoder handle the header and footer of the uuencode format. Just the body, bestie. Anything else is on you. π
β BINARY FORMAT β
The binary format command generates a binary string whose layout is specified by the formatString and whose contents come from the additional arguments. The resulting binary value is returned.
The formatString consists of a sequence of zero or more field specifiers separated by zero or more spaces. Each field specifier is a single type character followed by an optional flag character followed by an optional numeric count. Most field specifiers consume one argument to obtain the value to be formatted. The type character specifies how the value is to be formatted. The count typically indicates how many items of the specified type are taken from the value. If present, the count is a non-negative decimal integer or *, which normally indicates that all of the items in the value are to be used. If the number of arguments does not match the number of fields in the format string that consume arguments, then an error is generated. The flag character is ignored for binary format. yeah. she saw it and said "not today." π
Here is a small example to clarify the relation between the field specifiers and the arguments:
binary format d3d {1.0 2.0 3.0 4.0} 0.1
The first argument is a list of four numbers, but because of the count of 3 for the associated field specifier, only the first three will be used. The second argument is associated with the second field specifier. The resulting binary string contains the four numbers 1.0, 2.0, 3.0 and 0.1.
Each type-count pair moves an imaginary cursor through the binary data, storing bytes at the current position and advancing the cursor to just after the last byte stored. The cursor is initially at position 0 at the beginning of the data. main character energy from the very first byte. π
The type may be any one of the following characters:
aβ Stores a byte string of length count in the output string. Every character is taken as modulo 256 (i.e. the low byte of every character is used, and the high byte discarded) so when storing character strings not wholly expressible using the characters\u0000-\u00ff, theencoding converttocommand should be used first to change the string into an external representation if this truncation is not desired (i.e. if the characters are not part of the ISO 8859-1 character set). If arg has fewer than count bytes, then additional zero bytes are used to pad out the field. If arg is longer than the specified length, the extra characters will be ignored. If count is*, then all of the bytes in arg will be formatted. If count is omitted, then one character will be formatted. For example, the command:
binary format a7a*a alpha bravo charlie
will return a binary string equivalent to:
alpha\000\000bravoc
the command:
binary format a* [encoding convertto utf-8 \u20ac]
will return a binary string equivalent to:
\342\202\254
(which is the UTF-8 byte sequence for a Euro-currency character), and the command:
binary format a* [encoding convertto iso8859-15 \u20ac]
will return a binary string equivalent to:
\244
(which is the ISO 8859-15 byte sequence for a Euro-currency character). Contrast these last two with:
binary format a* \u20ac
which returns a binary string equivalent to:
\254
(i.e. \xAC) by truncating the high-bits of the character, and which is probably not what is desired. bestie. literally do the encoding convertto. please. π
Aβ This form is the same asaexcept that spaces are used for padding instead of nulls. For example,
binary format A6A*A alpha bravo charlie
will return
alpha bravoc
bβ Stores a string of count binary digits in low-to-high order within each byte in the output binary string. Arg must contain a sequence of1and0characters. The resulting bytes are emitted in first to last order with the bits being formatted in low-to-high order within each byte. If arg has fewer than count digits, then zeros will be used for the remaining bits. If arg has more than the specified number of digits, the extra digits will be ignored. If count is*, then all of the digits in arg will be formatted. If count is omitted, then one digit will be formatted. If the number of bits formatted does not end at a byte boundary, the remaining bits of the last byte will be zeros. For example,
binary format b5b* 11100 111000011010
will return a binary string equivalent to:
\x07\x87\x05
Bβ This form is the same asbexcept that the bits are stored in high-to-low order within each byte. For example,
binary format B5B* 11100 111000011010
will return a binary string equivalent to:
\xE0\xE1\xA0
H β hex digits, packed high-to-low β¨
Bestie, this one takes a string of count hexadecimal digits and stores them high-to-low inside each byte of the output binary string. Arg has to contain a sequence of characters from the set β0123456789abcdefABCDEFβ and nothing else, okay?
The resulting bytes get emitted in first-to-last order, with the hex digits formatted high-to-low within each byte. The deets:
- If
arghas fewer thancountdigits, zeros fill in the remaining digits πΈ - If
arghas more than the specified number of digits, the extra digits are just ignored, no drama π - If
countisβ*β, then all of the digits inargget formatted - If
countis omitted, then one digit gets formatted - If the number of digits formatted doesn't land on a byte boundary, the remaining bits of the last byte will be zeros
Like, for example:
binary format H3H*H2 ab DEF 987
will return a binary string equivalent to:
\xAB\x00\xDE\xF0\x98
h β the same but flipped π
This form is the same as H except that the digits are stored low-to-high order within each byte. Real talk, this is seldom required. For example:
binary format h3h*h2 AB def 987
will return a binary string equivalent to:
\xBA\x00\xED\x0F\x89
c β little 8-bit integers, living their best life π
Stores one or more 8-bit integer values in the output string.
- If no
countis specified, thenargmust consist of an integer value - If
countis specified,argmust consist of a list containing at least that many integers
The low-order 8 bits of each integer are stored as a one-byte value at the cursor position. If count is β*β, then all of the integers in the list are formatted. If the number of elements in the list is greater than count, then the extra elements are ignored, bye π
For example:
binary format c3cc* {3 -3 128 1} 260 {2 5}
will return a binary string equivalent to:
\x03\xFD\x80\x04\x02\x05
whereas:
binary format c {2 5}
will generate an error π (yeah, because c with no count wants one single integer, not a whole list).
s β 16-bit little-endian integers π§
This form is the same as c except that it stores one or more 16-bit integers in little-endian byte order in the output string. The low-order 16-bits of each integer are stored as a two-byte value at the cursor position, with the least significant byte stored first. For example:
binary format s3 {3 -3 258 1}
will return a binary string equivalent to:
\x03\x00\xFD\xFF\x02\x01
S β 16-bit big-endian integers π
This form is the same as s except that it stores one or more 16-bit integers in big-endian byte order in the output string. For example:
binary format S3 {3 -3 258 1}
will return a binary string equivalent to:
\x00\x03\xFF\xFD\x01\x02
t β mnemonically tiny π
This form (mnemonically tiny) is the same as s and S except that it stores the 16-bit integers in the output string in the native byte order of the machine where the Tcl script is running. Wanna know what your machine's native byte order is? Go peek at the byteOrder element of the tcl_platform array, iykyk π
i β 32-bit little-endian integers π₯
This form is the same as c except that it stores one or more 32-bit integers in little-endian byte order in the output string. The low-order 32-bits of each integer are stored as a four-byte value at the cursor position, with the least significant byte stored first. For example:
binary format i3 {3 -3 65536 1}
will return a binary string equivalent to:
\x03\x00\x00\x00\xFD\xFF\xFF\xFF\x00\x00\x01\x00
I β 32-bit big-endian integers π
This form is the same as i except that it stores one or more 32-bit integers in big-endian byte order in the output string. For example:
binary format I3 {3 -3 65536 1}
will return a binary string equivalent to:
\x00\x00\x00\x03\xFF\xFF\xFF\xFD\x00\x01\x00\x00
n β mnemonically number or normal β
This form (mnemonically number or normal) is the same as i and I except that it stores the 32-bit integers in the output string in the native byte order of the machine where the Tcl script is running. To figure out what that native byte order actually is, refer to the byteOrder element of the tcl_platform array π«Ά
w β 64-bit little-endian integers π
This form is the same as c except that it stores one or more 64-bit integers in little-endian byte order in the output string. The low-order 64-bits of each integer are stored as an eight-byte value at the cursor position, with the least significant byte stored first. For example:
binary format w 7810179016327718216
will return the binary string HelloTcl. cute, right? π
W β 64-bit big-endian integers π¦
This form is the same as w except that it stores one or more 64-bit integers in big-endian byte order in the output string. For example:
binary format Wc 4785469626960341345 110
will return the binary string BigEndian. iconic.
m β mnemonically the mirror of w πͺ
This form (mnemonically the mirror of w) is the same as w and W except that it stores the 64-bit integers in the output string in the native byte order of the machine where the Tcl script is running. Same deal as always: to determine the native byte order of the machine, refer to the byteOrder element of the tcl_platform array πΈ
f β single-precision floats, machine's native vibe π
This form is the same as c except that it stores one or more single-precision floating point numbers in the machine's native representation in the output string. Listen, this representation is not portable across architectures, so do not use it to send floating point numbers across the network, okay bestie? π«
- The size of a floating point number may vary across architectures, so the number of bytes that are generated may vary
- If the value overflows the machine's native representation, then the value of
FLT_MAXas defined by the system will be used instead - Because Tcl uses double-precision floating point numbers internally, there may be some loss of precision in the conversion to single-precision π
For example, on a Windows system running on an Intel Pentium processor:
binary format f2 {1.6 3.4}
will return a binary string equivalent to:
\xCD\xCC\xCC\x3F\x9A\x99\x59\x40
r β mnemonically real π«
This form (mnemonically real) is the same as f except that it stores the single-precision floating point numbers in little-endian order. Heads up though: this conversion only produces meaningful output when used on machines which use the IEEE floating point representation (very common, but not universal) π
R β big-endian single-precision π‘
This form is the same as r except that it stores the single-precision floating point numbers in big-endian order.
d β double-precision floats, native style β¨
This form is the same as f except that it stores one or more double-precision floating point numbers in the machine's native representation in the output string. For example, on a Windows system running on an Intel Pentium processor:
binary format d1 {1.6}
will return a binary string equivalent to:
\x9A\x99\x99\x99\x99\x99\xF9\x3F
q β mnemonically the mirror of d πͺ
This form (mnemonically the mirror of d) is the same as d except that it stores the double-precision floating point numbers in little-endian order. And yeah, same caveat as its siblings: this conversion only produces meaningful output when used on machines which use the IEEE floating point representation (very common, but not universal) π
Q β big-endian double-precision π
This form is the same as q except that it stores the double-precision floating point numbers in big-endian order.
x β null bytes, no thoughts just vibes π«§
Stores count null bytes in the output string.
- If
countis not specified, stores one null byte - If
countisβ*β, generates an error π - This type does not consume an argument
For example:
binary format a3xa3x2a3 abc def ghi
will return a binary string equivalent to:
abc\000def\000\000ghi
X β cursor, but make it go backwards βͺ
Moves the cursor back count bytes in the output string.
- If
countisβ*βor is larger than the current cursor position, then the cursor is positioned at location0so that the next byte stored will be the first byte in the result string - If
countis omitted then the cursor is moved back one byte - This type does not consume an argument
For example:
binary format a3X*a3X2a3 abc def ghi
will return dghi.
@ β cursor teleport, absolute edition π
Moves the cursor to the absolute location in the output string specified by count. Position 0 refers to the first byte in the output string.
- If
countrefers to a position beyond the last byte stored so far, then null bytes will be placed in the uninitialized locations and the cursor will be placed at the specified location - If
countisβ*β, then the cursor is moved to the current end of the output string - If
countis omitted, then an error will be generated π - This type does not consume an argument
For example:
binary format a5@2a1@*a3@10a1 abcde f ghi j
will return
abfdeghi\000\000j
π BINARY SCAN π
The binary scan command parses fields from a binary string, returning the number of conversions performed. String gives the input bytes to be parsed and formatString indicates how to parse it. An error is raised if string is anything other than a valid binary data value. Each varName gives the name of a variable; when a field is scanned from string the result is assigned to the corresponding variable π«Ά
As with binary format, the formatString consists of a sequence of zero or more field specifiers separated by zero or more spaces. Each field specifier is a single type character followed by an optional flag character followed by an optional numeric count. Most field specifiers consume one argument to obtain the variable into which the scanned values should be placed. The type character specifies how the binary data is to be interpreted. The count typically indicates how many items of the specified type are taken from the data. If present, the count is a non-negative decimal integer or β*β, which normally indicates that all of the remaining items in the data are to be used π
Now, the important bits, bestie β pay attention π
- If there are not enough bytes left after the current cursor position to satisfy the current field specifier, then the corresponding variable is left untouched and
binary scanreturns immediately with the number of variables that were set - If there are not enough arguments for all of the fields in the format string that consume arguments, then an error is generated π
- The flag character
βuβmay be given to cause some types to be read as unsigned values. The flag is accepted for all field types but is ignored for non-integer fields
A similar example as with binary format should explain the relation between field specifiers and arguments in case of the binary scan subcommand:
binary scan $bytes s3s first second
okay so bestie β¨ β provided the binary string chilling in the variable bytes is long enough π β this command hands a whole list of three integers to the variable first and a single value to the variable second. But here's the tea π«: if bytes contains fewer than 8 bytes (i.e. four 2-byte integers), then no assignment to second will be made, and if bytes contains fewer than 6 bytes (i.e. three 2-byte integers), no assignment to first will be made. Hence:
puts [binary scan abcdefg s3s first second]
puts $first
puts $second
will print (assuming neither variable is set previously):
1
25185 25699 26213
can't read "second": no such variable
π that can't read "second": no such variable is literally the vibe check failing in real time. not me getting emotionally attached to a variable that was never assigned π
It is important to note β and fr fr, sit with this one β that the c, s, and S (and i and I on 64bit systems) will be scanned into long data size values. And in doing this, values that have their high bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints), will be sign extended π. Thus the following will occur:
set signShort [binary format s1 0x8000]
binary scan $signShort s1 val; # val == 0xFFFF8000
If you require unsigned values β no cap, valid, iconic π
β you can include the βuβ flag character following the field type. For example, to read an unsigned short value:
set signShort [binary format s1 0x8000]
binary scan $signShort su1 val; # val == 0x00008000
Each type-count pair moves an imaginary cursor through the binary data, reading bytes from the current position π¦. The cursor is initially at position 0 at the beginning of the data. The type may be any one of the following characters:
aβ the data is a byte string of length count. If count isβ*β, then all of the remaining bytes in string will be scanned into the variable. If count is omitted, then one byte will be scanned. All bytes scanned will be interpreted as being characters in the range\u0000-\u00ff, so theencoding convertfromcommand will be needed if the string is not a binary string or a string encoded in ISO 8859-1. For example,
binary scan abcde\000fghi a6a10 var1 var2
will return 1 with the string equivalent to abcde\000 stored in var1 and var2 left unmodified π΄ (var2 got zero attention, sorry babe), and
binary scan \342\202\254 a* var1
set var2 [encoding convertfrom utf-8 $var1]
will store a Euro-currency character in var2 πΆβ¨.
Aβ this form is the same asa, except trailing blanks and nulls are stripped from the scanned value before it is stored in the variable. For example,
binary scan "abc efghi \000" A* var1
will return 1 with abc efghi stored in var1. squeaky clean, no crumbs π§Ή
bβ the data is turned into a string of count binary digits in low-to-high order represented as a sequence ofβ1βandβ0βcharacters. The data bytes are scanned in first to last order with the bits being taken in low-to-high order within each byte. Any extra bits in the last byte are ignored. If count isβ*β, then all of the remaining bits in string will be scanned. If count is omitted, then one bit will be scanned. For example,
binary scan \x07\x87\x05 b5b* var1 var2
will return 2 with 11100 stored in var1 and 1110000110100000 stored in var2 π€―
Bβ this form is the same asb, except the bits are taken in high-to-low order within each byte. For example,
binary scan \x70\x87\x05 B5B* var1 var2
will return 2 with 01110 stored in var1 and 1000011100000101 stored in var2.
Cβ this form is similar toA, except that it scans the data from start and terminates at the first null (C string semantics). For example,
binary scan "abc\000efghi" C* var1
will return 1 with abc stored in var1. it said "stop at the null" and the null was like okay periodt π
Hβ the data is turned into a string of count hexadecimal digits in high-to-low order represented as a sequence of characters in the setβ0123456789abcdefβ. The data bytes are scanned in first to last order with the hex digits being taken in high-to-low order within each byte. Any extra bits in the last byte are ignored. If count isβ*β, then all of the remaining hex digits in string will be scanned. If count is omitted, then one hex digit will be scanned. For example,
binary scan \x07\xC6\x05\x1F\x34 H3H* var1 var2
will return 2 with 07c stored in var1 and 051f34 stored in var2.
hβ this form is the same asH, except the digits are taken in reverse (low-to-high) order within each byte. For example,
binary scan \x07\x86\x05\x12\x34 h3h* var1 var2
will return 2 with 706 stored in var1 and 502143 stored in var2.
Note that most code that wishes to parse the hexadecimal digits from multiple bytes in order should use the H format. π important!! use H, bestie, i'm begging
cβ the data is turned into count 8-bit signed integers and stored in the corresponding variable as a list, or as unsigned ifuis placed immediately after thec. If count isβ*β, then all of the remaining bytes in string will be scanned. If count is omitted, then one 8-bit integer will be scanned. For example,
binary scan \x07\x86\x05 c2c* var1 var2
will return 2 with 7 -122 stored in var1 and 5 stored in var2. Note that the integers returned are signed unless cu in place of c.
sβ the data is interpreted as count 16-bit signed integers represented in little-endian byte order, or as unsigned ifuis placed immediately after thes. The integers are stored in the corresponding variable as a list. If count isβ*β, then all of the remaining bytes in string will be scanned. If count is omitted, then one 16-bit integer will be scanned. For example,
binary scan \x05\x00\x07\x00\xF0\xFF s2s* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2. Note that the integers returned are signed unless su is used in place of s.
Sβ this form is the same assexcept that the data is interpreted as count 16-bit integers represented in big-endian byte order. For example,
binary scan \x00\x05\x00\x07\xFF\xF0 S2S* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2.
tβ the data is interpreted as count 16-bit signed integers represented in the native byte order of the machine running the Tcl script, or as unsigned ifuis placed immediately after thet. It is otherwise identical tosandS. To determine what the native byte order of the machine is, refer to thebyteOrderelement of thetcl_platformarray π§iβ the data is interpreted as count 32-bit signed integers represented in little-endian byte order, or as unsigned ifuis placed immediately after thei. The integers are stored in the corresponding variable as a list. If count isβ*β, then all of the remaining bytes in string will be scanned. If count is omitted, then one 32-bit integer will be scanned. For example,
set str \x05\x00\x00\x00\x07\x00\x00\x00\xF0\xFF\xFF\xFF
binary scan $str i2i* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2. Note that the integers returned are signed unless iu is used in place of i.
Iβ this form is the same asIexcept that the data is interpreted as count 32-bit signed integers represented in big-endian byte order, or as unsigned ifuis placed immediately after theI. For example,
set str \x00\x00\x00\x05\x00\x00\x00\x07\xFF\xFF\xFF\xF0
binary scan $str I2I* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2.
π okay yes the page literally says "the same asI" while describingIβ that's the original's own little typo, i'm just the messenger, keeping it exactly as written bestie, don't @ me
nβ the data is interpreted as count 32-bit signed integers represented in the native byte order of the machine running the Tcl script, or as unsigned ifuis placed immediately after then. It is otherwise identical toiandI. To determine what the native byte order of the machine is, refer to thebyteOrderelement of thetcl_platformarray π§wβ the data is interpreted as count 64-bit signed integers represented in little-endian byte order, or as unsigned ifuis placed immediately after thew. The integers are stored in the corresponding variable as a list. If count isβ*β, then all of the remaining bytes in string will be scanned. If count is omitted, then one 64-bit integer will be scanned. For example,
set str \x05\x00\x00\x00\x07\x00\x00\x00\xF0\xFF\xFF\xFF
binary scan $str wi* var1 var2
will return 2 with 30064771077 stored in var1 and -16 stored in var2.
Wβ this form is the same aswexcept that the data is interpreted as count 64-bit signed integers represented in big-endian byte order, or as unsigned ifuis placed immediately after theW. For example,
set str \x00\x00\x00\x05\x00\x00\x00\x07\xFF\xFF\xFF\xF0
binary scan $str WI* var1 var2
will return 2 with 21474836487 stored in var1 and -16 stored in var2.
mβ the data is interpreted as count 64-bit signed integers represented in the native byte order of the machine running the Tcl script, or as unsigned ifuis placed immediately after them. It is otherwise identical towandW. To determine what the native byte order of the machine is, refer to thebyteOrderelement of thetcl_platformarray π§fβ the data is interpreted as count single-precision floating point numbers in the machine's native representation π. The floating point numbers are stored in the corresponding variable as a list. If count isβ*β, then all of the remaining bytes in string will be scanned. If count is omitted, then one single-precision floating point number will be scanned. The size of a floating point number may vary across architectures, so the number of bytes that are scanned may vary π. If the data does not represent a valid floating point number, the resulting value is undefined and compiler dependent π β yes that's a real warning, treat it with respect. For example, on a MS-Windows system running on an x86/x64 processor,
binary scan \xCD\xCC\xCC\x3F f var1
will return 1 with 1.6000000238418579 stored in var1.
rβ this form is the same asfexcept that the data is interpreted as count single-precision floating point number in little-endian order π. This conversion is not portable to the minority of systems not using IEEE floating point representations.Rβ literallyfbut the data gets read as count single-precision floats in big-endian order. Heads up bestie: this one is not portable to that tiny minority of systems not rocking IEEE floating point representations. πdβ same vibe asf, except the data is read as count double-precision floats in the machine's native representation. Like, for example, on a Windows system running on an Intel Pentium processor:
binary scan \x9A\x99\x99\x99\x99\x99\xF9\x3F d var1
returns 1 with 1.6000000000000001 stored in var1. Yes the float decided to be dramatic about it, that's just what it is. π
qβ same asdexcept the data gets read as count double-precision floats in little-endian order. And yeah, same warning as the others: not portable to the minority of systems not using IEEE floating point representations. πQβ same asdexcept the data gets read as count double-precision floats in big-endian order. Also not portable to that minority of non-IEEE systems. No cap.xβ moves the cursor forward count bytes in string. If count is*or is larger than the number of bytes after the current cursor position, then the cursor gets parked right after the last byte in string. If count is omitted, the cursor just scoots forward one byte. And note bestie β this type does not consume an argument. For example:
binary scan \x01\x02\x03\x04 x2H* var1
returns 1 with 0304 stored in var1. slay. β¨
Xβ moves the cursor back count bytes in string. If count is*or is larger than the current cursor position, the cursor is positioned at location0so the next byte scanned is the first byte in string. If count is omitted, the cursor backs up one byte. Again, this type does not consume an argument. For example:
binary scan \x01\x02\x03\x04 c2XH* var1 var2
returns 2 with 1 2 stored in var1 and 020304 stored in var2. iconic behaviour honestly. π«Ά
@β moves the cursor to the absolute location in the data string specified by count. Note that position0refers to the first byte in string. If count points past the end of string, the cursor goes to just after the last byte. And if count is omitted β an error will be generated, so don't do that. For example:
binary scan \x01\x02\x03\x04 c2@1H* var1 var2
returns 2 with 1 2 stored in var1 and 020304 stored in var2. π
π PORTABILITY ISSUES π
okay so this is important, listen up. The r, R, q and Q conversions will only work reliably for transferring data between computers that are all using IEEE floating point representations. This is very common, but not universal. π«
If you need to move floating-point numbers portably between all architectures, use their textual representation instead β the kind produced by format. periodt.
π EXAMPLES π
Here's a procedure that writes a Tcl string to a binary-encoded channel as UTF-8 data, preceded by a length word. chef's kiss, we love a length prefix:
proc writeString {channel string} {
set data [encoding convertto utf-8 $string]
puts -nonewline [binary format Ia* \
[string length $data] $data]
}
And this one reads a string back from a channel that was written by the writeString procedure above β the matching half of the duo, they're giving main character energy together:
proc readString {channel} {
if {![binary scan [read $channel 4] I length]} {
error "missing length"
}
set data [read $channel $length]
return [encoding convertfrom utf-8 $data]
}
This next one converts the contents of a file (named in the variable filename) to base64 and prints them:
set f [open $filename rb]
set data [read $f]
close $f
puts [binary encode base64 -maxlen 64 $data]
π SEE ALSO π
π KEYWORDS π
binary, format, scan
and that's the whole page bestie, we ate and left no crumbs π
β¨ this one is Tcl 8.0, dated binary(3tcl), and you're officially the group chat's resident binary whisperer now. go forth and scan responsibly π¦π
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1997 Sun Microsystems, Inc
- Copyright (c) 2008 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms