1binary(n)                    Tcl Built-In Commands                   binary(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       binary - Insert and extract fields from binary strings
9

SYNOPSIS

11       binary decode format ?-option value ...? data
12       binary encode format ?-option value ...? data
13       binary format formatString ?arg arg ...?
14       binary scan string formatString ?varName varName ...?
15______________________________________________________________________________
16

DESCRIPTION

18       This  command  provides  facilities  for manipulating binary data.  The
19       subcommand binary format creates a binary string from normal  Tcl  val‐
20       ues.   For  example,  given the values 16 and 22, on a 32-bit architec‐
21       ture, it might produce an 8-byte binary string consisting of two 4-byte
22       integers,  one  for  each  of the numbers.  The subcommand binary scan,
23       does the opposite: it extracts data from a binary string and returns it
24       as  ordinary  Tcl  string  values.  The binary encode and binary decode 
25       subcommands convert binary data to or from  string  encodings  such  as │
26       base64 (used in MIME messages for example).
27
28       Note that other operations on binary data, such as taking a subsequence
29       of it, getting its length, or reinterpreting it as a string in some en‐
30       coding,  are  done  by  other  Tcl commands (respectively string range,
31       string length and encoding convertfrom in the example cases).  A binary
32       string in Tcl is merely one where all the characters it contains are in
33       the range \u0000-\u00FF.
34

BINARY ENCODE AND DECODE

36       When encoding binary data as a readable  string,  the  starting  binary │
37       data  is passed to the binary encode command, together with the name of │
38       the encoding to use and any  encoding-specific  options  desired.  Data │
39       which  has  been encoded can be converted back to binary form using bi‐ 
40       nary decode. The following formats and options are supported.           │
41
42       base64                                                                  
43              The base64 binary encoding is commonly used in mail messages and │
44              XML  documents, and uses mostly upper and lower case letters and │
45              digits. It has the distinction of being able to be rewrapped ar‐ │
46              bitrarily without losing information.                            │
47
48              During encoding, the following options are supported:            │
49
50              -maxlen length                                                   │
51                     Indicates  that  the output should be split into lines of │
52                     no more than length characters. By default, lines are not │
53                     split.                                                    │
54
55              -wrapchar character                                              │
56                     Indicates  that,  when  lines  are  split  because of the │
57                     -maxlen option, character  should  be  used  to  separate │
58                     lines. By default, this is a newline character, “\n”.     │
59
60              During decoding, the following options are supported:            │
61
62              -strict                                                          
63                     Instructs  the decoder to throw an error if it encounters │
64                     any characters that are not strictly part of the encoding │
65                     itself.  Otherwise  it  ignores them.  RFC 2045 calls for │
66                     base64 decoders to be non-strict.                         │
67
68       hex                                                                     
69              The hex binary encoding converts each byte to a pair of hexadec‐ │
70              imal digits in big-endian form.                                  │
71
72              No  options  are supported during encoding. During decoding, the │
73              following options are supported:                                 │
74
75              -strict                                                          
76                     Instructs the decoder to throw an error if it  encounters │
77                     whitespace characters.  Otherwise it ignores them.        │
78
79       uuencode                                                                
80              The  uuencode  binary encoding used to be common for transfer of │
81              data between Unix systems and on  USENET,  but  is  less  common │
82              these  days, having been largely superseded by the base64 binary │
83              encoding.                                                        │
84
85              During encoding, the following  options  are  supported  (though │
86              changing  them  may  produce files that other implementations of │
87              decoders cannot process):                                        │
88
89              -maxlen length                                                   │
90                     Indicates the maximum number of characters to produce for │
91                     each  encoded  line.   The  valid  range is 5 to 85. Line │
92                     lengths outside that range cannot be accommodated by  the │
93                     encoding format. The default value is 61.                 │
94
95              -wrapchar character                                              │
96                     Indicates the character(s) to use to mark the end of each │
97                     encoded line.  Acceptable values are a sequence  of  zero │
98                     or  more characters from the set { \x09 (TAB), \x0B (VT), │
99                     \x0C (FF), \x0D (CR) } followed by zero  or  one  newline │
100                     \x0A  (LF).   Any  other values are rejected because they │
101                     would generate encoded text that could  not  be  decoded. │
102                     The default value is a single newline.                    │
103
104              During decoding, the following options are supported:            │
105
106              -strict                                                          
107                     Instructs  the decoder to throw an error if it encounters │
108                     anything outside of the standard encoding format. Without │
109                     this  option,  the  decoder  tolerates  some  deviations, │
110                     mostly to forgive reflows of lines  between  the  encoder │
111                     and decoder.                                              │
112
113              Note  that neither the encoder nor the decoder handle the header │
114              and footer of the uuencode format.                               │
115

BINARY FORMAT

117       The binary format command generates a binary  string  whose  layout  is
118       specified  by  the  formatString and whose contents come from the addi‐
119       tional arguments.  The resulting binary value is returned.
120
121       The formatString consists of a sequence of zero or  more  field  speci‐
122       fiers separated by zero or more spaces.  Each field specifier is a sin‐
123       gle type character followed by an optional flag character  followed  by
124       an  optional numeric count.  Most field specifiers consume one argument
125       to obtain the value to be formatted.  The type character specifies  how
126       the  value  is to be formatted.  The count typically indicates how many
127       items of the specified type are taken from the value.  If present,  the
128       count  is a non-negative decimal integer or *, which normally indicates
129       that all of the items in the value are to be used.  If  the  number  of
130       arguments does not match the number of fields in the format string that
131       consume arguments, then an error is generated. The  flag  character  is
132       ignored for binary format.
133
134       Here is a small example to clarify the relation between the field spec‐
135       ifiers and the arguments:
136              binary format d3d {1.0 2.0 3.0 4.0} 0.1
137
138       The first argument is a list of four numbers, but because of the  count
139       of  3  for the associated field specifier, only the first three will be
140       used. The second argument is associated with the  second  field  speci‐
141       fier.  The  resulting binary string contains the four numbers 1.0, 2.0,
142       3.0 and 0.1.
143
144       Each type-count pair moves an imaginary cursor through the binary data,
145       storing  bytes at the current position and advancing the cursor to just
146       after the last byte stored.  The cursor is initially at position  0  at
147       the  beginning  of  the data.  The type may be any one of the following
148       characters:
149
150       a    Stores a byte string of length count in the output string.   Every
151            character is taken as modulo 256 (i.e. the low byte of every char‐
152            acter is used, and the high byte discarded) so when storing  char‐
153            acter   strings   not  wholly  expressible  using  the  characters
154            \u0000-\u00ff, the encoding convertto command should be used first
155            to change the string into an external representation if this trun‐
156            cation is not desired (i.e. if the characters are not part of  the
157            ISO  8859-1  character  set.)   If arg has fewer than count bytes,
158            then additional zero bytes are used to pad out the field.  If  arg
159            is  longer than the specified length, the extra characters will be
160            ignored.  If count is *, then all of the bytes in arg will be for‐
161            matted.   If  count is omitted, then one character will be format‐
162            ted.  For example,
163                   binary format a7a*a alpha bravo charlie
164            will return a string equivalent to alpha\000\000bravoc,
165                   binary format a* [encoding convertto utf-8 \u20ac]
166            will return a string equivalent  to  \342\202\254  (which  is  the
167            UTF-8 byte sequence for a Euro-currency character) and
168                   binary format a* [encoding convertto iso8859-15 \u20ac]
169            will  return a string equivalent to \244 (which is the ISO 8859-15
170            byte sequence for a Euro-currency character). Contrast these  last
171            two with:
172                   binary format a* \u20ac
173            which  returns a string equivalent to \254 (i.e. \xac) by truncat‐
174            ing the high-bits of the character, and which is probably not what
175            is desired.
176
177       A    This form is the same as a except that spaces are used for padding
178            instead of nulls.  For example,
179                   binary format A6A*A alpha bravo charlie
180            will return alpha bravoc.
181
182       b    Stores a string of count binary digits in low-to-high order within
183            each  byte in the output string.  Arg must contain a sequence of 1
184            and 0 characters.  The resulting bytes are  emitted  in  first  to
185            last  order  with  the  bits  being formatted in low-to-high order
186            within each byte.  If arg has fewer than count digits, then  zeros
187            will  be  used  for  the remaining bits.  If arg has more than the
188            specified number of digits, the extra digits will be ignored.   If
189            count  is  *, then all of the digits in arg will be formatted.  If
190            count is omitted, then one digit will be formatted.  If the number
191            of  bits  formatted does not end at a byte boundary, the remaining
192            bits of the last byte will be zeros.  For example,
193                   binary format b5b* 11100 111000011010
194            will return a string equivalent to \x07\x87\x05.
195
196       B    This form is the same as b except that  the  bits  are  stored  in
197            high-to-low order within each byte.  For example,
198                   binary format B5B* 11100 111000011010
199            will return a string equivalent to \xe0\xe1\xa0.
200
201       H    Stores  a string of count hexadecimal digits in high-to-low within
202            each byte in the output string.  Arg must contain  a  sequence  of
203            characters  in  the  set  “0123456789abcdefABCDEF”.  The resulting
204            bytes are emitted in first to last order with the hex digits being
205            formatted in high-to-low order within each byte.  If arg has fewer
206            than count digits, then zeros will be used for the remaining  dig‐
207            its.  If arg has more than the specified number of digits, the ex‐
208            tra digits will be ignored.  If count is *, then all of the digits
209            in  arg  will  be  formatted.  If count is omitted, then one digit
210            will be formatted.  If the number of digits formatted does not end
211            at  a  byte  boundary, the remaining bits of the last byte will be
212            zeros.  For example,
213                   binary format H3H*H2 ab DEF 987
214            will return a string equivalent to \xab\x00\xde\xf0\x98.
215
216       h    This form is the same as H except that the digits  are  stored  in
217            low-to-high  order  within each byte. This is seldom required. For
218            example,
219                   binary format h3h*h2 AB def 987
220            will return a string equivalent to \xba\x00\xed\x0f\x89.
221
222       c    Stores one or more 8-bit integer values in the output string.   If
223            no  count is specified, then arg must consist of an integer value.
224            If count is specified, arg must consist of a  list  containing  at
225            least that many integers. The low-order 8 bits of each integer are
226            stored as a one-byte value at the cursor position.  If count is *,
227            then  all of the integers in the list are formatted. If the number
228            of elements in the list is greater than count, then the extra ele‐
229            ments are ignored.  For example,
230                   binary format c3cc* {3 -3 128 1} 260 {2 5}
231            will  return  a  string  equivalent  to  \x03\xfd\x80\x04\x02\x05,
232            whereas
233                   binary format c {2 5}
234            will generate an error.
235
236       s    This form is the same as c except  that  it  stores  one  or  more
237            16-bit  integers in little-endian byte order in the output string.
238            The low-order 16-bits of each integer are  stored  as  a  two-byte
239            value  at  the  cursor  position  with  the least significant byte
240            stored first.  For example,
241                   binary format s3 {3 -3 258 1}
242            will return a string equivalent to \x03\x00\xfd\xff\x02\x01.
243
244       S    This form is the same as s except  that  it  stores  one  or  more
245            16-bit  integers  in  big-endian  byte order in the output string.
246            For example,
247                   binary format S3 {3 -3 258 1}
248            will return a string equivalent to \x00\x03\xff\xfd\x01\x02.
249
250       t    This form (mnemonically tiny) is the same as s and S  except  that
251            it  stores  the 16-bit integers in the output string in the native
252            byte order of the machine where the Tcl script is running.  To de‐
253            termine what the native byte order of the machine is, refer to the
254            byteOrder element of the tcl_platform array.
255
256       i    This form is the same as c except  that  it  stores  one  or  more
257            32-bit  integers in little-endian byte order in the output string.
258            The low-order 32-bits of each integer are stored  as  a  four-byte
259            value  at  the  cursor  position  with  the least significant byte
260            stored first.  For example,
261                   binary format i3 {3 -3 65536 1}
262            will       return       a       string        equivalent        to
263            \x03\x00\x00\x00\xfd\xff\xff\xff\x00\x00\x01\x00
264
265       I    This  form  is the same as i except that it stores one or more one
266            or more 32-bit integers in big-endian byte  order  in  the  output
267            string.  For example,
268                   binary format I3 {3 -3 65536 1}
269            will        return        a       string       equivalent       to
270            \x00\x00\x00\x03\xff\xff\xff\xfd\x00\x01\x00\x00
271
272       n    This form (mnemonically number or normal) is the same as i  and  I
273            except  that it stores the 32-bit integers in the output string in
274            the native byte order of the machine where the Tcl script is  run‐
275            ning.   To determine what the native byte order of the machine is,
276            refer to the byteOrder element of the tcl_platform array.
277
278       w    This form is the same as c except  that  it  stores  one  or  more
279            64-bit  integers in little-endian byte order in the output string.
280            The low-order 64-bits of each integer are stored as an  eight-byte
281            value  at  the  cursor  position  with  the least significant byte
282            stored first.  For example,
283                   binary format w 7810179016327718216
284            will return the string HelloTcl
285
286       W    This form is the same as w except that it stores one or  more  one
287            or  more  64-bit  integers  in big-endian byte order in the output
288            string.  For example,
289                   binary format Wc 4785469626960341345 110
290            will return the string BigEndian
291
292       m    This form (mnemonically the mirror of w) is the same as  w  and  W
293            except  that it stores the 64-bit integers in the output string in
294            the native byte order of the machine where the Tcl script is  run‐
295            ning.   To determine what the native byte order of the machine is,
296            refer to the byteOrder element of the tcl_platform array.
297
298       f    This form is the same as c except that it stores one or  more  one
299            or  more  single-precision floating point numbers in the machine's
300            native representation in the output string.   This  representation
301            is  not portable across architectures, so it should not be used to
302            communicate floating point numbers across the network.   The  size
303            of  a  floating point number may vary across architectures, so the
304            number of bytes that are generated may vary.  If the  value  over‐
305            flows  the  machine's  native  representation,  then  the value of
306            FLT_MAX as defined by the system will be  used  instead.   Because
307            Tcl uses double-precision floating point numbers internally, there
308            may be some loss of precision in the conversion  to  single-preci‐
309            sion.   For  example, on a Windows system running on an Intel Pen‐
310            tium processor,
311                   binary format f2 {1.6 3.4}
312            will       return       a       string        equivalent        to
313            \xcd\xcc\xcc\x3f\x9a\x99\x59\x40.
314
315       r    This  form  (mnemonically  real)  is  the same as f except that it
316            stores the single-precision floating point numbers  in  little-en‐
317            dian  order.  This conversion only produces meaningful output when
318            used on machines which use the IEEE floating point  representation
319            (very common, but not universal.)
320
321       R    This form is the same as r except that it stores the single-preci‐
322            sion floating point numbers in big-endian order.
323
324       d    This form is the same as f except that it stores one or  more  one
325            or  more  double-precision floating point numbers in the machine's
326            native representation in the output string.   For  example,  on  a
327            Windows system running on an Intel Pentium processor,
328                   binary format d1 {1.6}
329            will        return        a       string       equivalent       to
330            \x9a\x99\x99\x99\x99\x99\xf9\x3f.
331
332       q    This form (mnemonically the mirror of d) is the same as  d  except
333            that it stores the double-precision floating point numbers in lit‐
334            tle-endian order.  This conversion only produces meaningful output
335            when  used on machines which use the IEEE floating point represen‐
336            tation (very common, but not universal.)
337
338       Q    This form is the same as q except that it stores the double-preci‐
339            sion floating point numbers in big-endian order.
340
341       x    Stores  count  null  bytes  in the output string.  If count is not
342            specified, stores one null byte.  If count is *, generates an  er‐
343            ror.  This type does not consume an argument.  For example,
344                   binary format a3xa3x2a3 abc def ghi
345            will return a string equivalent to abc\000def\000\000ghi.
346
347       X    Moves  the cursor back count bytes in the output string.  If count
348            is * or is larger than the current cursor position, then the  cur‐
349            sor  is positioned at location 0 so that the next byte stored will
350            be the first byte in the result string.  If count is omitted  then
351            the  cursor is moved back one byte.  This type does not consume an
352            argument.  For example,
353                   binary format a3X*a3X2a3 abc def ghi
354            will return dghi.
355
356       @    Moves the cursor to the absolute location  in  the  output  string
357            specified  by  count.   Position 0 refers to the first byte in the
358            output string.  If count refers to a position beyond the last byte
359            stored so far, then null bytes will be placed in the uninitialized
360            locations and the cursor will be placed at the specified location.
361            If  count is *, then the cursor is moved to the current end of the
362            output string.  If count is omitted, then an error will be  gener‐
363            ated.  This type does not consume an argument. For example,
364                   binary format a5@2a1@*a3@10a1 abcde f ghi j
365            will return abfdeghi\000\000j.
366

BINARY SCAN

368       The  binary  scan command parses fields from a binary string, returning
369       the number of conversions performed.  String gives the input  bytes  to
370       be  parsed (one byte per character, and characters not representable as
371       a byte have their high bits chopped) and formatString indicates how  to
372       parse  it.   Each varName gives the name of a variable; when a field is
373       scanned from string the result is assigned to the  corresponding  vari‐
374       able.
375
376       As  with binary format, the formatString consists of a sequence of zero
377       or more field specifiers separated by zero or more spaces.  Each  field
378       specifier is a single type character followed by an optional flag char‐
379       acter followed by an optional numeric  count.   Most  field  specifiers
380       consume one argument to obtain the variable into which the scanned val‐
381       ues should be placed.  The type character specifies how the binary data
382       is  to be interpreted.  The count typically indicates how many items of
383       the specified type are taken from the data.  If present, the count is a
384       non-negative decimal integer or *, which normally indicates that all of
385       the remaining items in the data are to  be  used.   If  there  are  not
386       enough bytes left after the current cursor position to satisfy the cur‐
387       rent field specifier, then the corresponding variable is left untouched
388       and  binary  scan returns immediately with the number of variables that
389       were set.  If there are not enough arguments for all of the  fields  in
390       the  format  string that consume arguments, then an error is generated.
391       The flag character “u” may be given to cause some types to be  read  as
392       unsigned  values.  The  flag is accepted for all field types but is ig‐
393       nored for non-integer fields.
394
395       A similar example as with binary format should explain the relation be‐
396       tween field specifiers and arguments in case of the binary scan subcom‐
397       mand:
398              binary scan $bytes s3s first second
399
400       This command (provided the binary string in the variable bytes is  long
401       enough)  assigns a list of three integers to the variable first and as‐
402       signs a single value to the variable second.  If bytes  contains  fewer
403       than  8 bytes (i.e. four 2-byte integers), no assignment to second will
404       be made, and if bytes contains fewer than 6 bytes  (i.e.  three  2-byte
405       integers), no assignment to first will be made.  Hence:
406              puts [binary scan abcdefg s3s first second]
407              puts $first
408              puts $second
409       will print (assuming neither variable is set previously):
410              1
411              25185 25699 26213
412              can't read "second": no such variable
413
414       It is important to note that the c, s, and S (and i and I on 64bit sys‐
415       tems) will be scanned into long data size values.  In doing this,  val‐
416       ues  that  have  their high bit set (0x80 for chars, 0x8000 for shorts,
417       0x80000000 for ints), will be sign extended.  Thus the  following  will
418       occur:
419              set signShort [binary format s1 0x8000]
420              binary scan $signShort s1 val; # val == 0xFFFF8000
421       If  you  require unsigned values you can include the “u” flag character
422       following the field type. For example, to read an unsigned short value:
423              set signShort [binary format s1 0x8000]
424              binary scan $signShort su1 val; # val == 0x00008000
425
426       Each type-count pair moves an imaginary cursor through the binary data,
427       reading  bytes  from  the current position.  The cursor is initially at
428       position 0 at the beginning of the data.  The type may be  any  one  of
429       the following characters:
430
431       a    The  data  is  a byte string of length count.  If count is *, then
432            all of the remaining bytes in string  will  be  scanned  into  the
433            variable.   If  count  is  omitted, then one byte will be scanned.
434            All bytes scanned will be interpreted as being characters  in  the
435            range  \u0000-\u00ff  so  the encoding convertfrom command will be
436            needed if the string is not a binary string or a string encoded in
437            ISO 8859-1.  For example,
438                   binary scan abcde\000fghi a6a10 var1 var2
439            will  return  1  with the string equivalent to abcde\000 stored in
440            var1 and var2 left unmodified, and
441                   binary scan \342\202\254 a* var1
442                   set var2 [encoding convertfrom utf-8 $var1]
443            will store a Euro-currency character in var2.
444
445       A    This form is the same as a, except trailing blanks and  nulls  are
446            stripped  from  the scanned value before it is stored in the vari‐
447            able.  For example,
448                   binary scan "abc efghi  \000" A* var1
449            will return 1 with abc efghi stored in var1.
450
451       b    The data is turned into a string of count binary digits in low-to-
452            high  order  represented  as a sequence of “1” and “0” characters.
453            The data bytes are scanned in first to last order  with  the  bits
454            being taken in low-to-high order within each byte.  Any extra bits
455            in the last byte are ignored.  If count is *, then all of the  re‐
456            maining bits in string will be scanned.  If count is omitted, then
457            one bit will be scanned.  For example,
458                   binary scan \x07\x87\x05 b5b* var1 var2
459            will return 2 with  11100  stored  in  var1  and  1110000110100000
460            stored in var2.
461
462       B    This  form is the same as b, except the bits are taken in high-to-
463            low order within each byte.  For example,
464                   binary scan \x70\x87\x05 B5B* var1 var2
465            will return 2 with  01110  stored  in  var1  and  1000011100000101
466            stored in var2.
467
468       H    The  data  is  turned into a string of count hexadecimal digits in
469            high-to-low order represented as a sequence of characters  in  the
470            set  “0123456789abcdef”.   The  data bytes are scanned in first to
471            last order with the hex digits being taken  in  high-to-low  order
472            within  each byte. Any extra bits in the last byte are ignored. If
473            count is *, then all of the remaining hex digits in string will be
474            scanned.  If count is omitted, then one hex digit will be scanned.
475            For example,
476                   binary scan \x07\xC6\x05\x1f\x34 H3H* var1 var2
477            will return 2 with 07c stored in var1 and 051f34 stored in var2.
478
479       h    This form is the same as H, except the digits are taken in reverse
480            (low-to-high) order within each byte. For example,
481                   binary scan \x07\x86\x05\x12\x34 h3h* var1 var2
482            will return 2 with 706 stored in var1 and 502143 stored in var2.
483
484            Note  that  most  code that wishes to parse the hexadecimal digits
485            from multiple bytes in order should use the H format.
486
487       c    The data is turned into count 8-bit signed integers and stored  in
488            the  corresponding  variable as a list. If count is *, then all of
489            the remaining bytes in string will be scanned.  If count is  omit‐
490            ted, then one 8-bit integer will be scanned.  For example,
491                   binary scan \x07\x86\x05 c2c* var1 var2
492            will  return  2  with  7 -122 stored in var1 and 5 stored in var2.
493            Note that the integers returned are signed, but they can  be  con‐
494            verted to unsigned 8-bit quantities using an expression like:
495                   set num [expr { $num & 0xFF }]
496
497       s    The  data  is  interpreted  as count 16-bit signed integers repre‐
498            sented in little-endian byte order.  The integers  are  stored  in
499            the  corresponding variable as a list.  If count is *, then all of
500            the remaining bytes in string will be scanned.  If count is  omit‐
501            ted, then one 16-bit integer will be scanned.  For example,
502                   binary scan \x05\x00\x07\x00\xf0\xff s2s* var1 var2
503            will  return  2  with  5  7 stored in var1 and -16 stored in var2.
504            Note that the integers returned are signed, but they can  be  con‐
505            verted to unsigned 16-bit quantities using an expression like:
506                   set num [expr { $num & 0xFFFF }]
507
508       S    This  form is the same as s except that the data is interpreted as
509            count 16-bit signed integers represented in big-endian byte order.
510            For example,
511                   binary scan \x00\x05\x00\x07\xff\xf0 S2S* var1 var2
512            will return 2 with 5 7 stored in var1 and -16 stored in var2.
513
514       t    The  data  is  interpreted  as count 16-bit signed integers repre‐
515            sented in the native byte order of the  machine  running  the  Tcl
516            script.   It is otherwise identical to s and S.  To determine what
517            the native byte order of the machine is, refer  to  the  byteOrder
518            element of the tcl_platform array.
519
520       i    The  data  is  interpreted  as count 32-bit signed integers repre‐
521            sented in little-endian byte order.  The integers  are  stored  in
522            the  corresponding variable as a list.  If count is *, then all of
523            the remaining bytes in string will be scanned.  If count is  omit‐
524            ted, then one 32-bit integer will be scanned.  For example,
525                   set str \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff
526                   binary scan $str i2i* var1 var2
527            will  return  2  with  5  7 stored in var1 and -16 stored in var2.
528            Note that the integers returned are signed, but they can  be  con‐
529            verted to unsigned 32-bit quantities using an expression like:
530                   set num [expr { $num & 0xFFFFFFFF }]
531
532       I    This  form is the same as I except that the data is interpreted as
533            count 32-bit signed integers represented in big-endian byte order.
534            For example,
535                   set str \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0
536                   binary scan $str I2I* var1 var2
537            will return 2 with 5 7 stored in var1 and -16 stored in var2.
538
539       n    The  data  is  interpreted  as count 32-bit signed integers repre‐
540            sented in the native byte order of the  machine  running  the  Tcl
541            script.   It is otherwise identical to i and I.  To determine what
542            the native byte order of the machine is, refer  to  the  byteOrder
543            element of the tcl_platform array.
544
545       w    The  data  is  interpreted  as count 64-bit signed integers repre‐
546            sented in little-endian byte order.  The integers  are  stored  in
547            the  corresponding variable as a list.  If count is *, then all of
548            the remaining bytes in string will be scanned.  If count is  omit‐
549            ted, then one 64-bit integer will be scanned.  For example,
550                   set str \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff
551                   binary scan $str wi* var1 var2
552            will  return  2  with 30064771077 stored in var1 and -16 stored in
553            var2.  Note that the integers returned are signed  and  cannot  be
554            represented by Tcl as unsigned values.
555
556       W    This  form is the same as w except that the data is interpreted as
557            count 64-bit signed integers represented in big-endian byte order.
558            For example,
559                   set str \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0
560                   binary scan $str WI* var1 var2
561            will  return  2  with 21474836487 stored in var1 and -16 stored in
562            var2.
563
564       m    The data is interpreted as count  64-bit  signed  integers  repre‐
565            sented  in  the  native  byte order of the machine running the Tcl
566            script.  It is otherwise identical to w and W.  To determine  what
567            the  native  byte  order of the machine is, refer to the byteOrder
568            element of the tcl_platform array.
569
570       f    The data is interpreted as count single-precision  floating  point
571            numbers  in  the  machine's  native  representation.  The floating
572            point numbers are stored in the corresponding variable as a  list.
573            If  count  is *, then all of the remaining bytes in string will be
574            scanned.  If count is omitted, then one single-precision  floating
575            point number will be scanned.  The size of a floating point number
576            may vary across architectures, so the number  of  bytes  that  are
577            scanned may vary.  If the data does not represent a valid floating
578            point number, the resulting value is undefined and compiler depen‐
579            dent.   For  example, on a Windows system running on an Intel Pen‐
580            tium processor,
581                   binary scan \x3f\xcc\xcc\xcd f var1
582            will return 1 with 1.6000000238418579 stored in var1.
583
584       r    This form is the same as f except that the data is interpreted  as
585            count  single-precision floating point number in little-endian or‐
586            der.  This conversion is not portable to the minority  of  systems
587            not using IEEE floating point representations.
588
589       R    This  form is the same as f except that the data is interpreted as
590            count single-precision floating point number in big-endian  order.
591            This conversion is not portable to the minority of systems not us‐
592            ing IEEE floating point representations.
593
594       d    This form is the same as f except that the data is interpreted  as
595            count double-precision floating point numbers in the machine's na‐
596            tive representation. For example, on a Windows system  running  on
597            an Intel Pentium processor,
598                   binary scan \x9a\x99\x99\x99\x99\x99\xf9\x3f d var1
599            will return 1 with 1.6000000000000001 stored in var1.
600
601       q    This  form is the same as d except that the data is interpreted as
602            count double-precision floating point number in little-endian  or‐
603            der.   This  conversion is not portable to the minority of systems
604            not using IEEE floating point representations.
605
606       Q    This form is the same as d except that the data is interpreted  as
607            count  double-precision floating point number in big-endian order.
608            This conversion is not portable to the minority of systems not us‐
609            ing IEEE floating point representations.
610
611       x    Moves  the cursor forward count bytes in string.  If count is * or
612            is larger than the number of bytes after the current cursor  posi‐
613            tion, then the cursor is positioned after the last byte in string.
614            If count is omitted, then the cursor is moved  forward  one  byte.
615            Note that this type does not consume an argument.  For example,
616                   binary scan \x01\x02\x03\x04 x2H* var1
617            will return 1 with 0304 stored in var1.
618
619       X    Moves  the cursor back count bytes in string.  If count is * or is
620            larger than the current cursor position, then the cursor is  posi‐
621            tioned  at  location  0  so that the next byte scanned will be the
622            first byte in string.  If count is  omitted  then  the  cursor  is
623            moved  back one byte.  Note that this type does not consume an ar‐
624            gument.  For example,
625                   binary scan \x01\x02\x03\x04 c2XH* var1 var2
626            will return 2 with 1 2 stored in var1 and 020304 stored in var2.
627
628       @    Moves the cursor to the absolute location in the data string spec‐
629            ified  by count.  Note that position 0 refers to the first byte in
630            string.  If count refers to a position beyond the end  of  string,
631            then  the  cursor  is positioned after the last byte.  If count is
632            omitted, then an error will be generated.  For example,
633                   binary scan \x01\x02\x03\x04 c2@1H* var1 var2
634            will return 2 with 1 2 stored in var1 and 020304 stored in var2.
635

PORTABILITY ISSUES

637       The r, R, q and Q conversions will only work reliably for  transferring
638       data  between  computers which are all using IEEE floating point repre‐
639       sentations.  This is very  common,  but  not  universal.   To  transfer
640       floating-point  numbers  portably  between all architectures, use their
641       textual representation (as produced by format) instead.
642

EXAMPLES

644       This is a procedure to write a Tcl string to a  binary-encoded  channel
645       as UTF-8 data preceded by a length word:
646
647              proc writeString {channel string} {
648                  set data [encoding convertto utf-8 $string]
649                  puts -nonewline [binary format Ia* \
650                          [string length $data] $data]
651              }
652
653       This  procedure  reads  a string from a channel that was written by the
654       previously presented writeString procedure:
655
656              proc readString {channel} {
657                  if {![binary scan [read $channel 4] I length]} {
658                      error "missing length"
659                  }
660                  set data [read $channel $length]
661                  return [encoding convertfrom utf-8 $data]
662              }
663
664       This converts the contents of a file (named in the  variable  filename)
665       to base64 and prints them:
666
667              set f [open $filename rb]
668              set data [read $f]
669              close $f
670              puts [binary encode base64 -maxlen 64 $data]
671

SEE ALSO

673       encoding(n), format(n), scan(n), string(n), tcl_platform(n)
674

KEYWORDS

676       binary, format, scan
677
678
679
680Tcl                                   8.0                            binary(n)
Impressum