Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/binary.n @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 30.9 KB
Line 
1'\"
2'\" Copyright (c) 1997 by Sun Microsystems, Inc.
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: binary.n,v 1.38 2008/01/02 21:21:37 dkf Exp $
8'\"
9.so man.macros
10.TH binary n 8.0 Tcl "Tcl Built-In Commands"
11.BS
12'\" Note:  do not modify the .SH NAME line immediately below!
13.SH NAME
14binary \- Insert and extract fields from binary strings
15.SH SYNOPSIS
16\fBbinary format \fIformatString \fR?\fIarg arg ...\fR?
17.br
18\fBbinary scan \fIstring formatString \fR?\fIvarName varName ...\fR?
19.BE
20.SH DESCRIPTION
21.PP
22This command provides facilities for manipulating binary data.  The
23first form, \fBbinary format\fR, creates a binary string from normal
24Tcl values.  For example, given the values 16 and 22, on a 32-bit
25architecture, it might produce an 8-byte binary string consisting of
26two 4-byte integers, one for each of the numbers.  The second form of
27the command, \fBbinary scan\fR, does the opposite: it extracts data
28from a binary string and returns it as ordinary Tcl string values.
29.SH "BINARY FORMAT"
30.PP
31The \fBbinary format\fR command generates a binary string whose layout
32is specified by the \fIformatString\fR and whose contents come from
33the additional arguments.  The resulting binary value is returned.
34.PP
35The \fIformatString\fR consists of a sequence of zero or more field
36specifiers separated by zero or more spaces.  Each field specifier is
37a single type character followed by an optional flag character followed
38by an optional numeric \fIcount\fR.
39Most field specifiers consume one argument to obtain the value to be
40formatted.  The type character specifies how the value is to be
41formatted.  The \fIcount\fR typically indicates how many items of the
42specified type are taken from the value.  If present, the \fIcount\fR
43is a non-negative decimal integer or \fB*\fR, which normally indicates
44that all of the items in the value are to be used.  If the number of
45arguments does not match the number of fields in the format string
46that consume arguments, then an error is generated. The flag character
47is ignored for for \fBbinary format\fR.
48.PP
49Here is a small example to clarify the relation between the field
50specifiers and the arguments:
51.CS
52\fBbinary format\fR d3d {1.0 2.0 3.0 4.0} 0.1
53.CE
54.PP
55The first argument is a list of four numbers, but because of the count
56of 3 for the associated field specifier, only the first three will be
57used. The second argument is associated with the second field
58specifier. The resulting binary string contains the four numbers 1.0,
592.0, 3.0 and 0.1.
60.PP
61Each type-count pair moves an imaginary cursor through the binary
62data, storing bytes at the current position and advancing the cursor
63to just after the last byte stored.  The cursor is initially at
64position 0 at the beginning of the data.  The type may be any one of
65the following characters:
66.IP \fBa\fR 5
67Stores a byte string of length \fIcount\fR in the output string.
68Every character is taken as modulo 256 (i.e. the low byte of every
69character is used, and the high byte discarded) so when storing
70character strings not wholly expressible using the characters \eu0000-\eu00ff,
71the \fBencoding convertto\fR command should be used first to change
72the string into an external representation
73if this truncation is not desired (i.e. if the characters are
74not part of the ISO 8859\-1 character set.)
75If \fIarg\fR has fewer than \fIcount\fR bytes, then additional zero
76bytes are used to pad out the field.  If \fIarg\fR is longer than the
77specified length, the extra characters will be ignored.  If
78\fIcount\fR is \fB*\fR, then all of the bytes in \fIarg\fR will be
79formatted.  If \fIcount\fR is omitted, then one character will be
80formatted.  For example,
81.RS
82.CS
83\fBbinary format\fR a7a*a alpha bravo charlie
84.CE
85will return a string equivalent to \fBalpha\e000\e000bravoc\fR,
86.CS
87\fBbinary format\fR a* [encoding convertto utf-8 \eu20ac]
88.CE
89will return a string equivalent to \fB\e342\e202\e254\fR (which is the
90UTF-8 byte sequence for a Euro-currency character) and
91.CS
92\fBbinary format\fR a* [encoding convertto iso8859-15 \eu20ac]
93.CE
94will return a string equivalent to \fB\e244\fR (which is the ISO
958859\-15 byte sequence for a Euro-currency character). Contrast these
96last two with:
97.CS
98\fBbinary format\fR a* \eu20ac
99.CE
100which returns a string equivalent to \fB\e254\fR (i.e. \fB\exac\fR) by
101truncating the high-bits of the character, and which is probably not
102what is desired.
103.RE
104.IP \fBA\fR 5
105This form is the same as \fBa\fR except that spaces are used for
106padding instead of nulls.  For example,
107.RS
108.CS
109\fBbinary format\fR A6A*A alpha bravo charlie
110.CE
111will return \fBalpha bravoc\fR.
112.RE
113.IP \fBb\fR 5
114Stores a string of \fIcount\fR binary digits in low-to-high order
115within each byte in the output string.  \fIArg\fR must contain a
116sequence of \fB1\fR and \fB0\fR characters.  The resulting bytes are
117emitted in first to last order with the bits being formatted in
118low-to-high order within each byte.  If \fIarg\fR has fewer than
119\fIcount\fR digits, then zeros will be used for the remaining bits.
120If \fIarg\fR has more than the specified number of digits, the extra
121digits will be ignored.  If \fIcount\fR is \fB*\fR, then all of the
122digits in \fIarg\fR will be formatted.  If \fIcount\fR is omitted,
123then one digit will be formatted.  If the number of bits formatted
124does not end at a byte boundary, the remaining bits of the last byte
125will be zeros.  For example,
126.RS
127.CS
128\fBbinary format\fR b5b* 11100 111000011010
129.CE
130will return a string equivalent to \fB\ex07\ex87\ex05\fR.
131.RE
132.IP \fBB\fR 5
133This form is the same as \fBb\fR except that the bits are stored in
134high-to-low order within each byte.  For example,
135.RS
136.CS
137\fBbinary format\fR B5B* 11100 111000011010
138.CE
139will return a string equivalent to \fB\exe0\exe1\exa0\fR.
140.RE
141.IP \fBH\fR 5
142Stores a string of \fIcount\fR hexadecimal digits in high-to-low
143within each byte in the output string.  \fIArg\fR must contain a
144sequence of characters in the set
145.QW 0123456789abcdefABCDEF .
146The resulting bytes are emitted in first to last order with the hex digits
147being formatted in high-to-low order within each byte.  If \fIarg\fR
148has fewer than \fIcount\fR digits, then zeros will be used for the
149remaining digits.  If \fIarg\fR has more than the specified number of
150digits, the extra digits will be ignored.  If \fIcount\fR is
151\fB*\fR, then all of the digits in \fIarg\fR will be formatted.  If
152\fIcount\fR is omitted, then one digit will be formatted.  If the
153number of digits formatted does not end at a byte boundary, the
154remaining bits of the last byte will be zeros.  For example,
155.RS
156.CS
157\fBbinary format\fR H3H*H2 ab DEF 987
158.CE
159will return a string equivalent to \fB\exab\ex00\exde\exf0\ex98\fR.
160.RE
161.IP \fBh\fR 5
162This form is the same as \fBH\fR except that the digits are stored in
163low-to-high order within each byte. This is seldom required. For example,
164.RS
165.CS
166\fBbinary format\fR h3h*h2 AB def 987
167.CE
168will return a string equivalent to \fB\exba\ex00\exed\ex0f\ex89\fR.
169.RE
170.IP \fBc\fR 5
171Stores one or more 8-bit integer values in the output string.  If no
172\fIcount\fR is specified, then \fIarg\fR must consist of an integer
173value. If \fIcount\fR is specified, \fIarg\fR must consist of a list
174containing at least that many integers. The low-order 8 bits of each integer
175are stored as a one-byte value at the cursor position.  If \fIcount\fR
176is \fB*\fR, then all of the integers in the list are formatted. If the
177number of elements in the list is greater
178than \fIcount\fR, then the extra elements are ignored.  For example,
179.RS
180.CS
181\fBbinary format\fR c3cc* {3 -3 128 1} 260 {2 5}
182.CE
183will return a string equivalent to
184\fB\ex03\exfd\ex80\ex04\ex02\ex05\fR, whereas
185.CS
186\fBbinary format\fR c {2 5}
187.CE
188will generate an error.
189.RE
190.IP \fBs\fR 5
191This form is the same as \fBc\fR except that it stores one or more
19216-bit integers in little-endian byte order in the output string.  The
193low-order 16-bits of each integer are stored as a two-byte value at
194the cursor position with the least significant byte stored first.  For
195example,
196.RS
197.CS
198\fBbinary format\fR s3 {3 -3 258 1}
199.CE
200will return a string equivalent to
201\fB\ex03\ex00\exfd\exff\ex02\ex01\fR.
202.RE
203.IP \fBS\fR 5
204This form is the same as \fBs\fR except that it stores one or more
20516-bit integers in big-endian byte order in the output string.  For
206example,
207.RS
208.CS
209\fBbinary format\fR S3 {3 -3 258 1}
210.CE
211will return a string equivalent to
212\fB\ex00\ex03\exff\exfd\ex01\ex02\fR.
213.RE
214.IP \fBt\fR 5
215.VS 8.5
216This form (mnemonically \fItiny\fR) is the same as \fBs\fR and \fBS\fR
217except that it stores the 16-bit integers in the output string in the
218native byte order of the machine where the Tcl script is running.
219To determine what the native byte order of the machine is, refer to
220the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
221.VE 8.5
222.IP \fBi\fR 5
223This form is the same as \fBc\fR except that it stores one or more
22432-bit integers in little-endian byte order in the output string.  The
225low-order 32-bits of each integer are stored as a four-byte value at
226the cursor position with the least significant byte stored first.  For
227example,
228.RS
229.CS
230\fBbinary format\fR i3 {3 -3 65536 1}
231.CE
232will return a string equivalent to
233\fB\ex03\ex00\ex00\ex00\exfd\exff\exff\exff\ex00\ex00\ex01\ex00\fR
234.RE
235.IP \fBI\fR 5
236This form is the same as \fBi\fR except that it stores one or more one
237or more 32-bit integers in big-endian byte order in the output string.
238For example,
239.RS
240.CS
241\fBbinary format\fR I3 {3 -3 65536 1}
242.CE
243will return a string equivalent to
244\fB\ex00\ex00\ex00\ex03\exff\exff\exff\exfd\ex00\ex01\ex00\ex00\fR
245.RE
246.IP \fBn\fR 5
247.VS 8.5
248This form (mnemonically \fInumber\fR or \fInormal\fR) is the same as
249\fBi\fR and \fBI\fR except that it stores the 32-bit integers in the
250output string in the native byte order of the machine where the Tcl
251script is running.
252To determine what the native byte order of the machine is, refer to
253the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
254.VE 8.5
255.IP \fBw\fR 5
256This form is the same as \fBc\fR except that it stores one or more
25764-bit integers in little-endian byte order in the output string.  The
258low-order 64-bits of each integer are stored as an eight-byte value at
259the cursor position with the least significant byte stored first.  For
260example,
261.RS
262.CS
263\fBbinary format\fR w 7810179016327718216
264.CE
265will return the string \fBHelloTcl\fR
266.RE
267.IP \fBW\fR 5
268This form is the same as \fBw\fR except that it stores one or more one
269or more 64-bit integers in big-endian byte order in the output string.
270For example,
271.RS
272.CS
273\fBbinary format\fR Wc 4785469626960341345 110
274.CE
275will return the string \fBBigEndian\fR
276.RE
277.IP \fBm\fR 5
278.VS 8.5
279This form (mnemonically the mirror of \fBw\fR) is the same as \fBw\fR
280and \fBW\fR except that it stores the 64-bit integers in the output
281string in the native byte order of the machine where the Tcl script is
282running.
283To determine what the native byte order of the machine is, refer to
284the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
285.VE 8.5
286.IP \fBf\fR 5
287This form is the same as \fBc\fR except that it stores one or more one
288or more single-precision floating point numbers in the machine's native
289representation in the output string.  This representation is not
290portable across architectures, so it should not be used to communicate
291floating point numbers across the network.  The size of a floating
292point number may vary across architectures, so the number of bytes
293that are generated may vary.  If the value overflows the
294machine's native representation, then the value of FLT_MAX
295as defined by the system will be used instead.  Because Tcl uses
296double-precision floating point numbers internally, there may be some
297loss of precision in the conversion to single-precision.  For example,
298on a Windows system running on an Intel Pentium processor,
299.RS
300.CS
301\fBbinary format\fR f2 {1.6 3.4}
302.CE
303will return a string equivalent to
304\fB\excd\excc\excc\ex3f\ex9a\ex99\ex59\ex40\fR.
305.RE
306.IP \fBr\fR 5
307.VS 8.5
308This form (mnemonically \fIreal\fR) is the same as \fBf\fR except that
309it stores the single-precision floating point numbers in little-endian
310order.  This conversion only produces meaningful output when used on
311machines which use the IEEE floating point representation (very
312common, but not universal.)
313.VE 8.5
314.IP \fBR\fR 5
315.VS 8.5
316This form is the same as \fBr\fR except that it stores the
317single-precision floating point numbers in big-endian order.
318.VE 8.5
319.IP \fBd\fR 5
320This form is the same as \fBf\fR except that it stores one or more one
321or more double-precision floating point numbers in the machine's native
322representation in the output string.  For example, on a
323Windows system running on an Intel Pentium processor,
324.RS
325.CS
326\fBbinary format\fR d1 {1.6}
327.CE
328will return a string equivalent to
329\fB\ex9a\ex99\ex99\ex99\ex99\ex99\exf9\ex3f\fR.
330.RE
331.IP \fBq\fR 5
332.VS 8.5
333This form (mnemonically the mirror of \fBd\fR) is the same as \fBd\fR
334except that it stores the double-precision floating point numbers in
335little-endian order.  This conversion only produces meaningful output
336when used on machines which use the IEEE floating point representation
337(very common, but not universal.)
338.VE 8.5
339.IP \fBQ\fR 5
340.VS 8.5
341This form is the same as \fBq\fR except that it stores the
342double-precision floating point numbers in big-endian order.
343.VE 8.5
344.IP \fBx\fR 5
345Stores \fIcount\fR null bytes in the output string.  If \fIcount\fR is
346not specified, stores one null byte.  If \fIcount\fR is \fB*\fR,
347generates an error.  This type does not consume an argument.  For
348example,
349.RS
350.CS
351\fBbinary format\fR a3xa3x2a3 abc def ghi
352.CE
353will return a string equivalent to \fBabc\e000def\e000\e000ghi\fR.
354.RE
355.IP \fBX\fR 5
356Moves the cursor back \fIcount\fR bytes in the output string.  If
357\fIcount\fR is \fB*\fR or is larger than the current cursor position,
358then the cursor is positioned at location 0 so that the next byte
359stored will be the first byte in the result string.  If \fIcount\fR is
360omitted then the cursor is moved back one byte.  This type does not
361consume an argument.  For example,
362.RS
363.CS
364\fBbinary format\fR a3X*a3X2a3 abc def ghi
365.CE
366will return \fBdghi\fR.
367.RE
368.IP \fB@\fR 5
369Moves the cursor to the absolute location in the output string
370specified by \fIcount\fR.  Position 0 refers to the first byte in the
371output string.  If \fIcount\fR refers to a position beyond the last
372byte stored so far, then null bytes will be placed in the uninitialized
373locations and the cursor will be placed at the specified location.  If
374\fIcount\fR is \fB*\fR, then the cursor is moved to the current end of
375the output string.  If \fIcount\fR is omitted, then an error will be
376generated.  This type does not consume an argument. For example,
377.RS
378.CS
379\fBbinary format\fR a5@2a1@*a3@10a1 abcde f ghi j
380.CE
381will return \fBabfdeghi\e000\e000j\fR.
382.RE
383.SH "BINARY SCAN"
384.PP
385The \fBbinary scan\fR command parses fields from a binary string,
386returning the number of conversions performed.  \fIString\fR gives the
387input bytes to be parsed (one byte per character, and characters not
388representable as a byte have their high bits chopped)
389and \fIformatString\fR indicates how to parse it.
390Each \fIvarName\fR gives the name of a variable; when a field is
391scanned from \fIstring\fR the result is assigned to the corresponding
392variable.
393.PP
394As with \fBbinary format\fR, the \fIformatString\fR consists of a
395sequence of zero or more field specifiers separated by zero or more
396spaces.  Each field specifier is a single type character followed by
397an optional flag character followed by an optional numeric \fIcount\fR.
398Most field specifiers consume one
399argument to obtain the variable into which the scanned values should
400be placed.  The type character specifies how the binary data is to be
401interpreted.  The \fIcount\fR typically indicates how many items of
402the specified type are taken from the data.  If present, the
403\fIcount\fR is a non-negative decimal integer or \fB*\fR, which
404normally indicates that all of the remaining items in the data are to
405be used.  If there are not enough bytes left after the current cursor
406position to satisfy the current field specifier, then the
407corresponding variable is left untouched and \fBbinary scan\fR returns
408immediately with the number of variables that were set.  If there are
409not enough arguments for all of the fields in the format string that
410consume arguments, then an error is generated. The flag character
411.QW u
412may be given to cause some types to be read as unsigned values. The flag
413is accepted for all field types but is ignored for non-integer fields.
414.PP
415A similar example as with \fBbinary format\fR should explain the
416relation between field specifiers and arguments in case of the binary
417scan subcommand:
418.CS
419\fBbinary scan\fR $bytes s3s first second
420.CE
421.PP
422This command (provided the binary string in the variable \fIbytes\fR
423is long enough) assigns a list of three integers to the variable
424\fIfirst\fR and assigns a single value to the variable \fIsecond\fR.
425If \fIbytes\fR contains fewer than 8 bytes (i.e. four 2-byte
426integers), no assignment to \fIsecond\fR will be made, and if
427\fIbytes\fR contains fewer than 6 bytes (i.e. three 2-byte integers),
428no assignment to \fIfirst\fR will be made.  Hence:
429.CS
430puts [\fBbinary scan\fR abcdefg s3s first second]
431puts $first
432puts $second
433.CE
434will print (assuming neither variable is set previously):
435.CS
4361
43725185 25699 26213
438can't read "second": no such variable
439.CE
440.PP
441It is \fIimportant\fR to note that the \fBc\fR, \fBs\fR, and \fBS\fR
442(and \fBi\fR and \fBI\fR on 64bit systems) will be scanned into
443long data size values.  In doing this, values that have their high
444bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints),
445will be sign extended.  Thus the following will occur:
446.CS
447set signShort [\fBbinary format\fR s1 0x8000]
448\fBbinary scan\fR $signShort s1 val; \fI# val == 0xFFFF8000\fR
449.CE
450If you require unsigned values you can include the
451.QW u
452flag character following
453the field type. For example, to read an unsigned short value:
454.CS
455set signShort [\fBbinary format\fR s1 0x8000]
456\fBbinary scan\fR $signShort su1 val; \fI# val == 0x00008000\fR
457.CE
458.PP
459Each type-count pair moves an imaginary cursor through the binary data,
460reading bytes from the current position.  The cursor is initially
461at position 0 at the beginning of the data.  The type may be any one of
462the following characters:
463.IP \fBa\fR 5
464The data is a byte string of length \fIcount\fR.  If \fIcount\fR
465is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be
466scanned into the variable.  If \fIcount\fR is omitted, then one
467byte will be scanned.
468All bytes scanned will be interpreted as being characters in the
469range \eu0000-\eu00ff so the \fBencoding convertfrom\fR command will be
470needed if the string is not a binary string or a string encoded in ISO
4718859\-1.
472For example,
473.RS
474.CS
475\fBbinary scan\fR abcde\e000fghi a6a10 var1 var2
476.CE
477will return \fB1\fR with the string equivalent to \fBabcde\e000\fR
478stored in \fIvar1\fR and \fIvar2\fR left unmodified, and
479.CS
480\fBbinary scan\fR \e342\e202\e254 a* var1
481set var2 [encoding convertfrom utf-8 $var1]
482.CE
483will store a Euro-currency character in \fIvar2\fR.
484.RE
485.IP \fBA\fR 5
486This form is the same as \fBa\fR, except trailing blanks and nulls are stripped from
487the scanned value before it is stored in the variable.  For example,
488.RS
489.CS
490\fBbinary scan\fR "abc efghi  \e000" A* var1
491.CE
492will return \fB1\fR with \fBabc efghi\fR stored in \fIvar1\fR.
493.RE
494.IP \fBb\fR 5
495The data is turned into a string of \fIcount\fR binary digits in
496low-to-high order represented as a sequence of
497.QW 1
498and
499.QW 0
500characters.  The data bytes are scanned in first to last order with
501the bits being taken in low-to-high order within each byte.  Any extra
502bits in the last byte are ignored.  If \fIcount\fR is \fB*\fR, then
503all of the remaining bits in \fIstring\fR will be scanned.  If
504\fIcount\fR is omitted, then one bit will be scanned.  For example,
505.RS
506.CS
507\fBbinary scan\fR \ex07\ex87\ex05 b5b* var1 var2
508.CE
509will return \fB2\fR with \fB11100\fR stored in \fIvar1\fR and
510\fB1110000110100000\fR stored in \fIvar2\fR.
511.RE
512.IP \fBB\fR 5
513This form is the same as \fBb\fR, except the bits are taken in
514high-to-low order within each byte.  For example,
515.RS
516.CS
517\fBbinary scan\fR \ex70\ex87\ex05 B5B* var1 var2
518.CE
519will return \fB2\fR with \fB01110\fR stored in \fIvar1\fR and
520\fB1000011100000101\fR stored in \fIvar2\fR.
521.RE
522.IP \fBH\fR 5
523The data is turned into a string of \fIcount\fR hexadecimal digits in
524high-to-low order represented as a sequence of characters in the set
525.QW 0123456789abcdef .
526The data bytes are scanned in first to last
527order with the hex digits being taken in high-to-low order within each
528byte. Any extra bits in the last byte are ignored. If \fIcount\fR is
529\fB*\fR, then all of the remaining hex digits in \fIstring\fR will be
530scanned. If \fIcount\fR is omitted, then one hex digit will be
531scanned. For example,
532.RS
533.CS
534\fBbinary scan\fR \ex07\exC6\ex05\ex1f\ex34 H3H* var1 var2
535.CE
536will return \fB2\fR with \fB07c\fR stored in \fIvar1\fR and
537\fB051f34\fR stored in \fIvar2\fR.
538.RE
539.IP \fBh\fR 5
540This form is the same as \fBH\fR, except the digits are taken in
541reverse (low-to-high) order within each byte. For example,
542.RS
543.CS
544\fBbinary scan\fR \ex07\ex86\ex05\ex12\ex34 h3h* var1 var2
545.CE
546will return \fB2\fR with \fB706\fR stored in \fIvar1\fR and
547\fB502143\fR stored in \fIvar2\fR.
548.RE
549Note that most code that wishes to parse the hexadecimal digits from
550multiple bytes in order should use the \fBH\fR format.
551.IP \fBc\fR 5
552The data is turned into \fIcount\fR 8-bit signed integers and stored
553in the corresponding variable as a list. If \fIcount\fR is \fB*\fR,
554then all of the remaining bytes in \fIstring\fR will be scanned.  If
555\fIcount\fR is omitted, then one 8-bit integer will be scanned.  For
556example,
557.RS
558.CS
559\fBbinary scan\fR \ex07\ex86\ex05 c2c* var1 var2
560.CE
561will return \fB2\fR with \fB7 -122\fR stored in \fIvar1\fR and \fB5\fR
562stored in \fIvar2\fR.  Note that the integers returned are signed, but
563they can be converted to unsigned 8-bit quantities using an expression
564like:
565.CS
566set num [expr { $num & 0xff }]
567.CE
568.RE
569.IP \fBs\fR 5
570The data is interpreted as \fIcount\fR 16-bit signed integers
571represented in little-endian byte order.  The integers are stored in
572the corresponding variable as a list.  If \fIcount\fR is \fB*\fR, then
573all of the remaining bytes in \fIstring\fR will be scanned.  If
574\fIcount\fR is omitted, then one 16-bit integer will be scanned.  For
575example,
576.RS
577.CS
578\fBbinary scan\fR \ex05\ex00\ex07\ex00\exf0\exff s2s* var1 var2
579.CE
580will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR
581stored in \fIvar2\fR.  Note that the integers returned are signed, but
582they can be converted to unsigned 16-bit quantities using an expression
583like:
584.CS
585set num [expr { $num & 0xffff }]
586.CE
587.RE
588.IP \fBS\fR 5
589This form is the same as \fBs\fR except that the data is interpreted
590as \fIcount\fR 16-bit signed integers represented in big-endian byte
591order.  For example,
592.RS
593.CS
594\fBbinary scan\fR \ex00\ex05\ex00\ex07\exff\exf0 S2S* var1 var2
595.CE
596will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR
597stored in \fIvar2\fR.
598.RE
599.IP \fBt\fR 5
600.VS 8.5
601The data is interpreted as \fIcount\fR 16-bit signed integers
602represented in the native byte order of the machine running the Tcl
603script.  It is otherwise identical to \fBs\fR and \fBS\fR.
604To determine what the native byte order of the machine is, refer to
605the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
606.VE 8.5
607.IP \fBi\fR 5
608The data is interpreted as \fIcount\fR 32-bit signed integers
609represented in little-endian byte order.  The integers are stored in
610the corresponding variable as a list.  If \fIcount\fR is \fB*\fR, then
611all of the remaining bytes in \fIstring\fR will be scanned.  If
612\fIcount\fR is omitted, then one 32-bit integer will be scanned.  For
613example,
614.RS
615.CS
616set str \ex05\ex00\ex00\ex00\ex07\ex00\ex00\ex00\exf0\exff\exff\exff
617\fBbinary scan\fR $str i2i* var1 var2
618.CE
619will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR
620stored in \fIvar2\fR.  Note that the integers returned are signed, but
621they can be converted to unsigned 32-bit quantities using an expression
622like:
623.CS
624set num [expr { $num & 0xffffffff }]
625.CE
626.RE
627.IP \fBI\fR 5
628This form is the same as \fBI\fR except that the data is interpreted
629as \fIcount\fR 32-bit signed integers represented in big-endian byte
630order.  For example,
631.RS
632.CS
633set str \ex00\ex00\ex00\ex05\ex00\ex00\ex00\ex07\exff\exff\exff\exf0
634\fBbinary scan\fR $str I2I* var1 var2
635.CE
636will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR
637stored in \fIvar2\fR.
638.RE
639.IP \fBn\fR 5
640.VS 8.5
641The data is interpreted as \fIcount\fR 32-bit signed integers
642represented in the native byte order of the machine running the Tcl
643script.  It is otherwise identical to \fBi\fR and \fBI\fR.
644To determine what the native byte order of the machine is, refer to
645the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
646.VE 8.5
647.IP \fBw\fR 5
648The data is interpreted as \fIcount\fR 64-bit signed integers
649represented in little-endian byte order.  The integers are stored in
650the corresponding variable as a list.  If \fIcount\fR is \fB*\fR, then
651all of the remaining bytes in \fIstring\fR will be scanned.  If
652\fIcount\fR is omitted, then one 64-bit integer will be scanned.  For
653example,
654.RS
655.CS
656set str \ex05\ex00\ex00\ex00\ex07\ex00\ex00\ex00\exf0\exff\exff\exff
657\fBbinary scan\fR $str wi* var1 var2
658.CE
659will return \fB2\fR with \fB30064771077\fR stored in \fIvar1\fR and
660\fB\-16\fR stored in \fIvar2\fR.  Note that the integers returned are
661signed and cannot be represented by Tcl as unsigned values.
662.RE
663.IP \fBW\fR 5
664This form is the same as \fBw\fR except that the data is interpreted
665as \fIcount\fR 64-bit signed integers represented in big-endian byte
666order.  For example,
667.RS
668.CS
669set str \ex00\ex00\ex00\ex05\ex00\ex00\ex00\ex07\exff\exff\exff\exf0
670\fBbinary scan\fR $str WI* var1 var2
671.CE
672will return \fB2\fR with \fB21474836487\fR stored in \fIvar1\fR and \fB\-16\fR
673stored in \fIvar2\fR.
674.RE
675.IP \fBm\fR 5
676.VS 8.5
677The data is interpreted as \fIcount\fR 64-bit signed integers
678represented in the native byte order of the machine running the Tcl
679script.  It is otherwise identical to \fBw\fR and \fBW\fR.
680To determine what the native byte order of the machine is, refer to
681the \fBbyteOrder\fR element of the \fBtcl_platform\fR array.
682.VE 8.5
683.IP \fBf\fR 5
684The data is interpreted as \fIcount\fR single-precision floating point
685numbers in the machine's native representation.  The floating point
686numbers are stored in the corresponding variable as a list.  If
687\fIcount\fR is \fB*\fR, then all of the remaining bytes in
688\fIstring\fR will be scanned.  If \fIcount\fR is omitted, then one
689single-precision floating point number will be scanned.  The size of a
690floating point number may vary across architectures, so the number of
691bytes that are scanned may vary.  If the data does not represent a
692valid floating point number, the resulting value is undefined and
693compiler dependent.  For example, on a Windows system running on an
694Intel Pentium processor,
695.RS
696.CS
697\fBbinary scan\fR \ex3f\excc\excc\excd f var1
698.CE
699will return \fB1\fR with \fB1.6000000238418579\fR stored in
700\fIvar1\fR.
701.RE
702.IP \fBr\fR 5
703.VS 8.5
704This form is the same as \fBf\fR except that the data is interpreted
705as \fIcount\fR single-precision floating point number in little-endian
706order.  This conversion is not portable to the minority of systems not
707using IEEE floating point representations.
708.VE 8.5
709.IP \fBR\fR 5
710.VS 8.5
711This form is the same as \fBf\fR except that the data is interpreted
712as \fIcount\fR single-precision floating point number in big-endian
713order.  This conversion is not portable to the minority of systems not
714using IEEE floating point representations.
715.VE 8.5
716.IP \fBd\fR 5
717This form is the same as \fBf\fR except that the data is interpreted
718as \fIcount\fR double-precision floating point numbers in the
719machine's native representation. For example, on a Windows system
720running on an Intel Pentium processor,
721.RS
722.CS
723\fBbinary scan\fR \ex9a\ex99\ex99\ex99\ex99\ex99\exf9\ex3f d var1
724.CE
725will return \fB1\fR with \fB1.6000000000000001\fR
726stored in \fIvar1\fR.
727.RE
728.IP \fBq\fR 5
729.VS 8.5
730This form is the same as \fBd\fR except that the data is interpreted
731as \fIcount\fR double-precision floating point number in little-endian
732order.  This conversion is not portable to the minority of systems not
733using IEEE floating point representations.
734.VE 8.5
735.IP \fBQ\fR 5
736.VS 8.5
737This form is the same as \fBd\fR except that the data is interpreted
738as \fIcount\fR double-precision floating point number in big-endian
739order.  This conversion is not portable to the minority of systems not
740using IEEE floating point representations.
741.VE 8.5
742.IP \fBx\fR 5
743Moves the cursor forward \fIcount\fR bytes in \fIstring\fR.  If
744\fIcount\fR is \fB*\fR or is larger than the number of bytes after the
745current cursor position, then the cursor is positioned after
746the last byte in \fIstring\fR.  If \fIcount\fR is omitted, then the
747cursor is moved forward one byte.  Note that this type does not
748consume an argument.  For example,
749.RS
750.CS
751\fBbinary scan\fR \ex01\ex02\ex03\ex04 x2H* var1
752.CE
753will return \fB1\fR with \fB0304\fR stored in \fIvar1\fR.
754.RE
755.IP \fBX\fR 5
756Moves the cursor back \fIcount\fR bytes in \fIstring\fR.  If
757\fIcount\fR is \fB*\fR or is larger than the current cursor position,
758then the cursor is positioned at location 0 so that the next byte
759scanned will be the first byte in \fIstring\fR.  If \fIcount\fR
760is omitted then the cursor is moved back one byte.  Note that this
761type does not consume an argument.  For example,
762.RS
763.CS
764\fBbinary scan\fR \ex01\ex02\ex03\ex04 c2XH* var1 var2
765.CE
766will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR
767stored in \fIvar2\fR.
768.RE
769.IP \fB@\fR 5
770Moves the cursor to the absolute location in the data string specified
771by \fIcount\fR.  Note that position 0 refers to the first byte in
772\fIstring\fR.  If \fIcount\fR refers to a position beyond the end of
773\fIstring\fR, then the cursor is positioned after the last byte.  If
774\fIcount\fR is omitted, then an error will be generated.  For example,
775.RS
776.CS
777\fBbinary scan\fR \ex01\ex02\ex03\ex04 c2@1H* var1 var2
778.CE
779will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR
780stored in \fIvar2\fR.
781.RE
782.SH "PORTABILITY ISSUES"
783The \fBr\fR, \fBR\fR, \fBq\fR and \fBQ\fR conversions will only work
784reliably for transferring data between computers which are all using
785IEEE floating point representations.  This is very common, but not
786universal.  To transfer floating-point numbers portably between all
787architectures, use their textual representation (as produced by
788\fBformat\fR) instead.
789.SH EXAMPLES
790This is a procedure to write a Tcl string to a binary-encoded channel as
791UTF-8 data preceded by a length word:
792.CS
793proc \fIwriteString\fR {channel string} {
794    set data [encoding convertto utf-8 $string]
795    puts -nonewline [\fBbinary format\fR Ia* \e
796            [string length $data] $data]
797}
798.CE
799.PP
800This procedure reads a string from a channel that was written by the
801previously presented \fIwriteString\fR procedure:
802.CS
803proc \fIreadString\fR {channel} {
804    if {![\fBbinary scan\fR [read $channel 4] I length]} {
805        error "missing length"
806    }
807    set data [read $channel $length]
808    return [encoding convertfrom utf-8 $data]
809}
810.CE
811.SH "SEE ALSO"
812format(n), scan(n), tclvars(n)
813.SH KEYWORDS
814binary, format, scan
Note: See TracBrowser for help on using the repository browser.