1mNAME0m
       binary - Insert and extract fields from binary strings

1mSYNOPSIS0m
       1mbinary format 4m22mformatString24m ?4marg24m 4marg24m 4m...24m?
       1mbinary scan 4m22mstring24m 4mformatString24m ?4mvarName24m 4mvarName24m 4m...24m?


1mDESCRIPTION0m
       This  command  provides  facilities  for manipulating binary data.  The
       first form, 1mbinary format22m, creates a binary string from normal Tcl val-
       ues.   For  example,  given the values 16 and 22, on a 32 bit architec-
       ture, it might produce an 8-byte binary string consisting of two 4-byte
       integers, one for each of the numbers.  The second form of the command,
       1mbinary scan22m, does the opposite: it extracts data from a  binary  string
       and returns it as ordinary Tcl string values.


1mBINARY FORMAT0m
       The  1mbinary  format  22mcommand  generates a binary string whose layout is
       specified by the 4mformatString24m and whose contents come  from  the  addi-
       tional arguments.  The resulting binary value is returned.

       The  4mformatString24m  consists  of a sequence of zero or more field speci-
       fiers separated by zero or more spaces.  Each field specifier is a sin-
       gle  type  character followed by an optional numeric 4mcount24m.  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
       4mcount24m typically indicates how many items  of  the  specified  type  are
       taken  from the value.  If present, the 4mcount24m is a non-negative decimal
       integer or 1m*22m, 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.

       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.  The type may be any one  of  the  following
       characters:

       1ma    22mStores  a  character  string of length 4mcount24m in the output string.
            If 4marg24m has fewer than 4mcount24m bytes, then additional zero bytes  are
            used  to  pad  out the field.  If 4marg24m is longer than the specified
            length, the extra characters will be ignored.  If 4mcount24m is 1m*22m, then
            all  of  the bytes in 4marg24m will be formatted.  If 4mcount24m is omitted,
            then one character will be formatted.  For example,
                   1mbinary format a7a*a alpha bravo charlie0m
            will return a string equivalent to 1malpha\000\000bravoc22m.

       1mA    22mThis form is the same as 1ma 22mexcept that spaces are used for padding
            instead of nulls.  For example,
                   1mbinary format A6A*A alpha bravo charlie0m
            will return 1malpha bravoc22m.

       1mb    22mStores a string of 4mcount24m binary digits in low-to-high order within
            each byte in the output string.  4mArg24m must contain a sequence of  1m10m
            and  1m0  22mcharacters.   The  resulting bytes are emitted in first to
            last order with the bits  being  formatted  in  low-to-high  order
            within  each byte.  If 4marg24m has fewer than 4mcount24m digits, then zeros
            will be used for the remaining bits.  If 4marg24m  has  more  than  the
            specified  number of digits, the extra digits will be ignored.  If
            4mcount24m is 1m*22m, then all of the digits in 4marg24m will be  formatted.   If
            4mcount24m 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,
                   1mbinary format b5b* 11100 1110000110100m
            will return a string equivalent to 1m\x07\x87\x0522m.

       1mB    22mThis  form  is  the  same  as 1mb 22mexcept that the bits are stored in
            high-to-low order within each byte.  For example,
                   1mbinary format B5B* 11100 1110000110100m
            will return a string equivalent to 1m\xe0\xe1\xa022m.

       1mh    22mStores a string of 4mcount24m hexadecimal digits in low-to-high  within
            each  byte  in  the output string.  4mArg24m must contain a sequence of
            characters in the set ``0123456789abcdefABCDEF''.   The  resulting
            bytes are emitted in first to last order with the hex digits being
            formatted in low-to-high order within each byte.  If 4marg24m has fewer
            than  4mcount24m digits, then zeros will be used for the remaining dig-
            its.  If 4marg24m has more than the specified  number  of  digits,  the
            extra digits will be ignored.  If 4mcount24m is 1m*22m, then all of the dig-
            its in 4marg24m will be formatted.  If 4mcount24m is omitted, then one digit
            will be formatted.  If the number of digits formatted does not end
            at a byte boundary, the remaining bits of the last  byte  will  be
            zeros.  For example,
                   1mbinary format h3h* AB def0m
            will return a string equivalent to 1m\xba\x00\xed\x0f22m.

       1mH    22mThis  form  is  the same as 1mh 22mexcept that the digits are stored in
            high-to-low order within each byte.  For example,
                   1mbinary format H3H* ab DEF0m
            will return a string equivalent to 1m\xab\x00\xde\xf022m.

       1mc    22mStores one or more 8-bit integer values in the output string.   If
            no  4mcount24m is specified, then 4marg24m must consist of an integer value;
            otherwise 4marg24m must consist of a list  containing  at  least  4mcount0m
            integer elements.  The low-order 8 bits of each integer are stored
            as a one-byte value at the cursor position.  If 4mcount24m is  1m*22m,  then
            all  of  the integers in the list are formatted.  If the number of
            elements in the list is fewer than 4mcount24m, then an error is  gener-
            ated.   If  the  number  of  elements  in the list is greater than
            4mcount24m, then the extra elements are ignored.  For example,
                   1mbinary format c3cc* {3 -3 128 1} 260 {2 5}0m
            will  return  a  string  equivalent  to  1m\x03\xfd\x80\x04\x02\x0522m,
            whereas
                   1mbinary format c {2 5}0m
            will generate an error.

       1ms    22mThis  form  is  the  same  as  1mc 22mexcept 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,
                   1mbinary format s3 {3 -3 258 1}0m
            will return a string equivalent to 1m\x03\x00\xfd\xff\x02\x0122m.

       1mS    22mThis  form  is  the  same  as  1ms 22mexcept that it stores one or more
            16-bit integers in big-endian byte order  in  the  output  string.
            For example,
                   1mbinary format S3 {3 -3 258 1}0m
            will return a string equivalent to 1m\x00\x03\xff\xfd\x01\x0222m.

       1mi    22mThis  form  is  the  same  as  1mc 22mexcept 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,
                   1mbinary format i3 {3 -3 65536 1}0m
            will        return        a       string       equivalent       to
            1m\x03\x00\x00\x00\xfd\xff\xff\xff\x00\x00\x01\x000m

       1mI    22mThis form is the same as 1mi 22mexcept that it stores one or  more  one
            or  more  32-bit  integers  in big-endian byte order in the output
            string.  For example,
                   1mbinary format I3 {3 -3 65536 1}0m
            will       return       a       string        equivalent        to
            1m\x00\x00\x00\x03\xff\xff\xff\xfd\x00\x01\x00\x000m

       1mf    22mThis  form  is the same as 1mc 22mexcept that it stores one or more one
            or more single-precision floating in the machine's  native  repre-
            sentation  in  the  output  string.   This  representation  is not
            portable across architectures, so it should not be used to  commu-
            nicate  floating  point numbers across the network.  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_MAX  as
            defined by the system will be used instead.  Because Tcl uses dou-
            ble-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  proces-
            sor,
                   1mbinary format f2 {1.6 3.4}0m
            will        return        a       string       equivalent       to
            1m\xcd\xcc\xcc\x3f\x9a\x99\x59\x4022m.

       1md    22mThis form is the same as 1mf 22mexcept that it stores one or  more  one
            or  more  double-precision floating in the machine's native repre-
            sentation in the output string.  For example, on a Windows  system
            running on an Intel Pentium processor,
                   1mbinary format d1 {1.6}0m
            will        return        a       string       equivalent       to
            1m\x9a\x99\x99\x99\x99\x99\xf9\x3f22m.

       1mx    22mStores 4mcount24m null bytes in the output string.   If  4mcount24m  is  not
            specified,  stores  one  null  byte.   If 4mcount24m is 1m*22m, generates an
            error.  This type does not consume an argument.  For example,
                   1mbinary format a3xa3x2a3 abc def ghi0m
            will return a string equivalent to 1mabc\000def\000\000ghi22m.

       1mX    22mMoves the cursor back 4mcount24m bytes in the output string.  If  4mcount0m
            is  1m* 22mor is larger than the current cursor position, then the cur-
            sor is positioned at location 0 so that the next byte stored  will
            be  the first byte in the result string.  If 4mcount24m is omitted then
            the cursor is moved back one byte.  This type does not consume  an
            argument.  For example,
                   1mbinary format a3X*a3X2a3 abc def ghi0m
            will return 1mdghi22m.

       1m@    22mMoves  the  cursor  to  the absolute location in the output string
            specified by 4mcount24m.  Position 0 refers to the first  byte  in  the
            output string.  If 4mcount24m refers to a position beyond the last byte
            stored so far, then null bytes will be placed in  the  unitialized
            locations and the cursor will be placed at the specified location.
            If 4mcount24m is 1m*22m, then the cursor is moved to the current end of  the
            output  string.  If 4mcount24m is omitted, then an error will be gener-
            ated.  This type does not consume an argument. For example,
                   1mbinary format a5@2a1@*a3@10a1 abcde f ghi j0m
            will return 1mabfdeghi\000\000j22m.


1mBINARY SCAN0m
       The 1mbinary scan 22mcommand parses fields from a binary  string,  returning
       the  number  of  conversions  performed.   4mString24m gives the input to be
       parsed and 4mformatString24m indicates how to parse it.  Each 4mvarName24m  gives
       the  name of a variable; when a field is scanned from 4mstring24m the result
       is assigned to the corresponding variable.

       As with 1mbinary format22m, the 4mformatString24m 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  numeric
       4mcount24m.   Most field specifiers consume one argument to obtain the vari-
       able into which the scanned values should be placed.  The type  charac-
       ter specifies how the binary data is to be interpreted.  The 4mcount24m typ-
       ically indicates how many items of the specified type  are  taken  from
       the  data.   If present, the 4mcount24m is a non-negative decimal integer or
       1m*22m, which normally indicates that all of the remaining items in the data
       are  to  be used.  If there are not enough bytes left after the current
       cursor position to satisfy the current field specifier, then the corre-
       sponding variable is left untouched and 1mbinary scan 22mreturns 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 argu-
       ments, then an error is generated.

       It is 1mimportant 22mto note that the 1mc22m, 1ms22m, and 1mS 22m(and 1mi 22mand 1mI 22mon 64bit sys-
       tems)  will be scanned into long data size values.  In doing this, val-
       ues that have their high bit set (0x80 for chars,  0x8000  for  shorts,
       0x80000000  for  ints), will be sign extended.  Thus the following will
       occur:
              1mset signShort [binary format s1 0x8000]0m
              1mbinary scan $signShort s1 val; 4m22m#24m 4mval24m 4m==24m 4m0xFFFF80000m
       If you want to produce an unsigned value, then you can mask the  return
       value  to  the desired size.  For example, to produce an unsigned short
       value:
              1mset val [expr {$val & 0xFFFF}]; 4m22m#24m 4mval24m 4m==24m 4m0x80000m

       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:

       1ma    22mThe  data  is  a character string of length 4mcount24m.  If 4mcount24m is 1m*22m,
            then all of the remaining bytes in 4mstring24m will be scanned into the
            variable.   If  4mcount24m  is  omitted,  then  one  character  will be
            scanned.  For example,
                   1mbinary scan abcde\000fghi a6a10 var1 var20m
            will return 1m1 22mwith the string equivalent to  1mabcde\000  22mstored  in
            1mvar1 22mand 1mvar2 22mleft unmodified.

       1mA    22mThis  form  is the same as 1ma22m, except trailing blanks and nulls are
            stripped from the scanned value before it is stored in  the  vari-
            able.  For example,
                   1mbinary scan "abc efghi  \000" A* var10m
            will return 1m1 22mwith 1mabc efghi 22mstored in 1mvar122m.

       1mb    22mThe data is turned into a string of 4mcount24m binary digits in low-to-
            high order represented as a sequence of ``1''  and  ``0''  charac-
            ters.   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 4mcount24m is 1m*22m, then all of the
            remaining bits in 1mstring 22mwill be scanned.  If  4mcount24m  is  omitted,
            then one bit will be scanned.  For example,
                   1mbinary scan \x07\x87\x05 b5b* var1 var20m
            will  return  1m2  22mwith  1m11100  22mstored  in 1mvar1 22mand 1m11100001101000000m
            stored in 1mvar222m.

       1mB    22mThis form is the same as 1mb22m, except the bits are taken in  high-to-
            low order within each byte.  For example,
                   1mbinary scan \x70\x87\x05 B5B* var1 var20m
            will  return  1m2  22mwith  1m01110  22mstored  in 1mvar1 22mand 1m10000111000001010m
            stored in 1mvar222m.

       1mh    22mThe data is turned into a string of 4mcount24m  hexadecimal  digits  in
            low-to-high  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 low-to-high order
            within each byte.  Any extra bits in the last  byte  are  ignored.
            If 4mcount24m is 1m*22m, then all of the remaining hex digits in 1mstring 22mwill
            be scanned.  If 4mcount24m is omitted,  then  one  hex  digit  will  be
            scanned.  For example,
                   1mbinary scan \x07\x86\x05 h3h* var1 var20m
            will return 1m2 22mwith 1m706 22mstored in 1mvar1 22mand 1m50 22mstored in 1mvar222m.

       1mH    22mThis  form  is the same as 1mh22m, except the digits are taken in high-
            to-low order within each byte.  For example,
                   1mbinary scan \x07\x86\x05 H3H* var1 var20m
            will return 1m2 22mwith 1m078 22mstored in 1mvar1 22mand 1m05 22mstored in 1mvar222m.

       1mc    22mThe data is turned into 4mcount24m 8-bit signed integers and stored  in
            the  corresponding  variable as a list. If 4mcount24m is 1m*22m, then all of
            the remaining bytes in 1mstring 22mwill be scanned.  If 4mcount24m is  omit-
            ted, then one 8-bit integer will be scanned.  For example,
                   1mbinary scan \x07\x86\x05 c2c* var1 var20m
            will  return  1m2  22mwith  1m7 -122 22mstored in 1mvar1 22mand 1m5 22mstored in 1mvar222m.
            Note that the integers returned are signed, but they can  be  con-
            verted to unsigned 8-bit quantities using an expression like:
                   1mexpr ( $num + 0x100 ) % 0x1000m

       1ms    22mThe  data  is  interpreted  as 4mcount24m 16-bit signed integers repre-
            sented in little-endian byte order.  The integers  are  stored  in
            the  corresponding variable as a list.  If 4mcount24m is 1m*22m, then all of
            the remaining bytes in 1mstring 22mwill be scanned.  If 4mcount24m is  omit-
            ted, then one 16-bit integer will be scanned.  For example,
                   1mbinary scan \x05\x00\x07\x00\xf0\xff s2s* var1 var20m
            will  return  1m2  22mwith  1m5  7 22mstored in 1mvar1 22mand 1m-16 22mstored in 1mvar222m.
            Note that the integers returned are signed, but they can  be  con-
            verted to unsigned 16-bit quantities using an expression like:
                   1mexpr ( $num + 0x10000 ) % 0x100000m

       1mS    22mThis  form is the same as 1ms 22mexcept that the data is interpreted as
            4mcount24m 16-bit signed integers represented in big-endian byte order.
            For example,
                   1mbinary scan \x00\x05\x00\x07\xff\xf0 S2S* var1 var20m
            will return 1m2 22mwith 1m5 7 22mstored in 1mvar1 22mand 1m-16 22mstored in 1mvar222m.

       1mi    22mThe  data  is  interpreted  as 4mcount24m 32-bit signed integers repre-
            sented in little-endian byte order.  The integers  are  stored  in
            the  corresponding variable as a list.  If 4mcount24m is 1m*22m, then all of
            the remaining bytes in 1mstring 22mwill be scanned.  If 4mcount24m is  omit-
            ted, then one 32-bit integer will be scanned.  For example,
                   1mbinary scan \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff i2i* var1 var20m
            will  return  1m2  22mwith  1m5  7 22mstored in 1mvar1 22mand 1m-16 22mstored in 1mvar222m.
            Note that the integers returned are signed and  cannot  be  repre-
            sented by Tcl as unsigned values.

       1mI    22mThis  form is the same as 1mI 22mexcept that the data is interpreted as
            4mcount24m 32-bit signed integers represented in big-endian byte order.
            For example,
                   1mbinary \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0 I2I* var1 var20m
            will return 1m2 22mwith 1m5 7 22mstored in 1mvar1 22mand 1m-16 22mstored in 1mvar222m.

       1mf    22mThe  data  is interpreted as 4mcount24m 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 4mcount24m is 1m*22m, then all of the remaining bytes in 1mstring  22mwill  be
            scanned.   If 4mcount24m 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 depen-
            dent.  For example, on a Windows system running on an  Intel  Pen-
            tium processor,
                   1mbinary scan \x3f\xcc\xcc\xcd f var10m
            will return 1m1 22mwith 1m1.6000000238418579 22mstored in 1mvar122m.

       1md    22mThis  form is the same as 1mf 22mexcept that the data is interpreted as
            4mcount24m double-precision floating point  numbers  in  the  machine's
            native representation. For example, on a Windows system running on
            an Intel Pentium processor,
                   1mbinary scan \x9a\x99\x99\x99\x99\x99\xf9\x3f d var10m
            will return 1m1 22mwith 1m1.6000000000000001 22mstored in 1mvar122m.

       1mx    22mMoves the cursor forward 4mcount24m bytes in 4mstring24m.  If 4mcount24m is 1m*  22mor
            is larger than the number of bytes after the current cursor cursor
            position, then the cursor is positioned after  the  last  byte  in
            4mstring24m.  If 4mcount24m is omitted, then the cursor is moved forward one
            byte.  Note that this type does  not  consume  an  argument.   For
            example,
                   1mbinary scan \x01\x02\x03\x04 x2H* var10m
            will return 1m1 22mwith 1m0304 22mstored in 1mvar122m.

       1mX    22mMoves  the cursor back 4mcount24m bytes in 4mstring24m.  If 4mcount24m is 1m* 22mor is
            larger than the current cursor position, then the cursor is  posi-
            tioned  at  location  0  so that the next byte scanned will be the
            first byte in 4mstring24m.  If 4mcount24m is  omitted  then  the  cursor  is
            moved  back  one  byte.   Note  that this type does not consume an
            argument.  For example,
                   1mbinary scan \x01\x02\x03\x04 c2XH* var1 var20m
            will return 1m2 22mwith 1m1 2 22mstored in 1mvar1 22mand 1m020304 22mstored in 1mvar222m.

       1m@    22mMoves the cursor to the absolute location in the data string spec-
            ified  by 4mcount24m.  Note that position 0 refers to the first byte in
            4mstring24m.  If 4mcount24m refers to a position beyond the end  of  4mstring24m,
            then  the  cursor  is positioned after the last byte.  If 4mcount24m is
            omitted, then an error will be generated.  For example,
                   1mbinary scan \x01\x02\x03\x04 c2@1H* var1 var20m
            will return 1m2 22mwith 1m1 2 22mstored in 1mvar1 22mand 1m020304 22mstored in 1mvar222m.


1mPLATFORM ISSUES0m
       Sometimes it is desirable to format  or  scan  integer  values  in  the
       native  byte  order for the machine.  Refer to the 1mbyteOrder 22melement of
       the 1mtcl_platform 22marray to decide which type character to use when  for-
       matting or scanning integers.


1mSEE ALSO0m
       format(n), scan(n), tclvars(n)


1mKEYWORDS0m
