binary.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:25k
- '"
- '" Copyright (c) 1997 by Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: binary.n,v 1.11.2.8 2005/02/10 10:28:21 dkf Exp $
- '"
- .so man.macros
- .TH binary n 8.0 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- binary - Insert and extract fields from binary strings
- .SH SYNOPSIS
- fBbinary format fIformatString fR?fIarg arg ...fR?
- .br
- fBbinary scan fIstring formatString fR?fIvarName varName ...fR?
- .BE
- .SH DESCRIPTION
- .PP
- This command provides facilities for manipulating binary data. The
- first form, fBbinary formatfR, creates a binary string from normal
- Tcl values. For example, given the values 16 and 22, on a 32 bit
- architecture, it might produce an 8-byte binary string consisting of
- two 4-byte integers, one for each of the numbers. The second form of
- the command, fBbinary scanfR, does the opposite: it extracts data
- from a binary string and returns it as ordinary Tcl string values.
- .SH "BINARY FORMAT"
- .PP
- The fBbinary formatfR command generates a binary string whose layout
- is specified by the fIformatStringfR and whose contents come from
- the additional arguments. The resulting binary value is returned.
- .PP
- The fIformatStringfR 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 fIcountfR.
- 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 fIcountfR typically indicates how many items of the
- specified type are taken from the value. If present, the fIcountfR
- is a non-negative decimal integer or fB*fR, 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.
- .PP
- Here is a small example to clarify the relation between the field
- specifiers and the arguments:
- .CS
- fBbinary format d3d {1.0 2.0 3.0 4.0} 0.1fR
- .CE
- .PP
- The first argument is a list of four numbers, but because of the count
- of 3 for the associated field specifier, only the first three will be
- used. The second argument is associated with the second field
- specifier. The resulting binary string contains the four numbers 1.0,
- 2.0, 3.0 and 0.1.
- .PP
- 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:
- .IP fBafR 5
- Stores a character string of length fIcountfR in the output string.
- Every character is taken as modulo 256 (i.e. the low byte of every
- character is used, and the high byte discarded) so when storing
- character strings not wholly expressible using the characters \u0000-\u00ff,
- the fBencoding converttofR command should be used
- first if this truncation is not desired (i.e. if the characters are
- not part of the ISO 8859-1 character set.)
- If fIargfR has fewer than fIcountfR bytes, then additional zero
- bytes are used to pad out the field. If fIargfR is longer than the
- specified length, the extra characters will be ignored. If
- fIcountfR is fB*fR, then all of the bytes in fIargfR will be
- formatted. If fIcountfR is omitted, then one character will be
- formatted. For example,
- .RS
- .CS
- fBbinary format a7a*a alpha bravo charliefR
- .CE
- will return a string equivalent to fBalpha\000\000bravocfR.
- .RE
- .IP fBAfR 5
- This form is the same as fBafR except that spaces are used for
- padding instead of nulls. For example,
- .RS
- .CS
- fBbinary format A6A*A alpha bravo charliefR
- .CE
- will return fBalpha bravocfR.
- .RE
- .IP fBbfR 5
- Stores a string of fIcountfR binary digits in low-to-high order
- within each byte in the output string. fIArgfR must contain a
- sequence of fB1fR and fB0fR characters. The resulting bytes are
- emitted in first to last order with the bits being formatted in
- low-to-high order within each byte. If fIargfR has fewer than
- fIcountfR digits, then zeros will be used for the remaining bits.
- If fIargfR has more than the specified number of digits, the extra
- digits will be ignored. If fIcountfR is fB*fR, then all of the
- digits in fIargfR will be formatted. If fIcountfR 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,
- .RS
- .CS
- fBbinary format b5b* 11100 111000011010fR
- .CE
- will return a string equivalent to fB\x07\x87\x05fR.
- .RE
- .IP fBBfR 5
- This form is the same as fBbfR except that the bits are stored in
- high-to-low order within each byte. For example,
- .RS
- .CS
- fBbinary format B5B* 11100 111000011010fR
- .CE
- will return a string equivalent to fB\xe0\xe1\xa0fR.
- .RE
- .IP fBhfR 5
- Stores a string of fIcountfR hexadecimal digits in low-to-high
- within each byte in the output string. fIArgfR 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 fIargfR
- has fewer than fIcountfR digits, then zeros will be used for the
- remaining digits. If fIargfR has more than the specified number of
- digits, the extra digits will be ignored. If fIcountfR is
- fB*fR, then all of the digits in fIargfR will be formatted. If
- fIcountfR 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,
- .RS
- .CS
- fBbinary format h3h* AB deffR
- .CE
- will return a string equivalent to fB\xba\x00\xed\x0ffR.
- .RE
- .IP fBHfR 5
- This form is the same as fBhfR except that the digits are stored in
- high-to-low order within each byte. For example,
- .RS
- .CS
- fBbinary format H3H* ab DEFfR
- .CE
- will return a string equivalent to fB\xab\x00\xde\xf0fR.
- .RE
- .IP fBcfR 5
- Stores one or more 8-bit integer values in the output string. If no
- fIcountfR is specified, then fIargfR must consist of an integer
- value; otherwise fIargfR must consist of a list containing at least
- fIcountfR integer elements. The low-order 8 bits of each integer
- are stored as a one-byte value at the cursor position. If fIcountfR
- is fB*fR, then all of the integers in the list are formatted. If
- the number of elements in the list is fewer than fIcountfR, then an
- error is generated. If the number of elements in the list is greater
- than fIcountfR, then the extra elements are ignored. For example,
- .RS
- .CS
- fBbinary format c3cc* {3 -3 128 1} 260 {2 5}fR
- .CE
- will return a string equivalent to
- fB\x03\xfd\x80\x04\x02\x05fR, whereas
- .CS
- fBbinary format c {2 5}fR
- .CE
- will generate an error.
- .RE
- .IP fBsfR 5
- This form is the same as fBcfR except that it stores one or more
- 16-bit integers in little-endian byte order in the output string. The
- low-order 16-bits of each integer are stored as a two-byte value at
- the cursor position with the least significant byte stored first. For
- example,
- .RS
- .CS
- fBbinary format s3 {3 -3 258 1}fR
- .CE
- will return a string equivalent to
- fB\x03\x00\xfd\xff\x02\x01fR.
- .RE
- .IP fBSfR 5
- This form is the same as fBsfR except that it stores one or more
- 16-bit integers in big-endian byte order in the output string. For
- example,
- .RS
- .CS
- fBbinary format S3 {3 -3 258 1}fR
- .CE
- will return a string equivalent to
- fB\x00\x03\xff\xfd\x01\x02fR.
- .RE
- .IP fBifR 5
- This form is the same as fBcfR except that it stores one or more
- 32-bit integers in little-endian byte order in the output string. The
- low-order 32-bits of each integer are stored as a four-byte value at
- the cursor position with the least significant byte stored first. For
- example,
- .RS
- .CS
- fBbinary format i3 {3 -3 65536 1}fR
- .CE
- will return a string equivalent to
- fB\x03\x00\x00\x00\xfd\xff\xff\xff\x00\x00\x01\x00fR
- .RE
- .IP fBIfR 5
- This form is the same as fBifR except that it stores one or more one
- or more 32-bit integers in big-endian byte order in the output string.
- For example,
- .RS
- .CS
- fBbinary format I3 {3 -3 65536 1}fR
- .CE
- will return a string equivalent to
- fB\x00\x00\x00\x03\xff\xff\xff\xfd\x00\x01\x00\x00fR
- .RE
- .IP fBwfR 5
- .VS 8.4
- This form is the same as fBcfR except that it stores one or more
- 64-bit integers in little-endian byte order in the output string. The
- low-order 64-bits of each integer are stored as an eight-byte value at
- the cursor position with the least significant byte stored first. For
- example,
- .RS
- .CS
- fBbinary format w 7810179016327718216fR
- .CE
- will return the string fBHelloTclfR
- .RE
- .IP fBWfR 5
- This form is the same as fBwfR except that it stores one or more one
- or more 64-bit integers in big-endian byte order in the output string.
- For example,
- .RS
- .CS
- fBbinary format Wc 4785469626960341345 110fR
- .CE
- will return the string fBBigEndianfR
- .VE
- .RE
- .IP fBffR 5
- This form is the same as fBcfR except that it stores one or more one
- or more single-precision floating in the machine's native
- representation in the output string. This representation is not
- portable across architectures, so it should not be used to communicate
- 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
- double-precision floating-point numbers internally, there may be some
- loss of precision in the conversion to single-precision. For example,
- on a Windows system running on an Intel Pentium processor,
- .RS
- .CS
- fBbinary format f2 {1.6 3.4}fR
- .CE
- will return a string equivalent to
- fB\xcd\xcc\xcc\x3f\x9a\x99\x59\x40fR.
- .RE
- .IP fBdfR 5
- This form is the same as fBffR except that it stores one or more one
- or more double-precision floating in the machine's native
- representation in the output string. For example, on a
- Windows system running on an Intel Pentium processor,
- .RS
- .CS
- fBbinary format d1 {1.6}fR
- .CE
- will return a string equivalent to
- fB\x9a\x99\x99\x99\x99\x99\xf9\x3ffR.
- .RE
- .IP fBxfR 5
- Stores fIcountfR null bytes in the output string. If fIcountfR is
- not specified, stores one null byte. If fIcountfR is fB*fR,
- generates an error. This type does not consume an argument. For
- example,
- .RS
- .CS
- fBbinary format a3xa3x2a3 abc def ghifR
- .CE
- will return a string equivalent to fBabc\000def\000\000ghifR.
- .RE
- .IP fBXfR 5
- Moves the cursor back fIcountfR bytes in the output string. If
- fIcountfR is fB*fR or is larger than the current cursor position,
- then the cursor is positioned at location 0 so that the next byte
- stored will be the first byte in the result string. If fIcountfR is
- omitted then the cursor is moved back one byte. This type does not
- consume an argument. For example,
- .RS
- .CS
- fBbinary format a3X*a3X2a3 abc def ghifR
- .CE
- will return fBdghifR.
- .RE
- .IP fB@fR 5
- Moves the cursor to the absolute location in the output string
- specified by fIcountfR. Position 0 refers to the first byte in the
- output string. If fIcountfR refers to a position beyond the last
- byte stored so far, then null bytes will be placed in the uninitialized
- locations and the cursor will be placed at the specified location. If
- fIcountfR is fB*fR, then the cursor is moved to the current end of
- the output string. If fIcountfR is omitted, then an error will be
- generated. This type does not consume an argument. For example,
- .RS
- .CS
- fBbinary format a5@2a1@*a3@10a1 abcde f ghi jfR
- .CE
- will return fBabfdeghi\000\000jfR.
- .RE
- .SH "BINARY SCAN"
- .PP
- The fBbinary scanfR command parses fields from a binary string,
- returning the number of conversions performed. fIStringfR gives the
- input to be parsed and fIformatStringfR indicates how to parse it.
- Each fIvarNamefR gives the name of a variable; when a field is
- scanned from fIstringfR the result is assigned to the corresponding
- variable.
- .PP
- As with fBbinary formatfR, the fIformatStringfR 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 fIcountfR. Most field specifiers consume one
- argument to obtain the variable into which the scanned values should
- be placed. The type character specifies how the binary data is to be
- interpreted. The fIcountfR typically indicates how many items of
- the specified type are taken from the data. If present, the
- fIcountfR is a non-negative decimal integer or fB*fR, 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
- corresponding variable is left untouched and fBbinary scanfR returns
- immediately with the number of variables that were set. If there are
- not enough arguments for all of the fields in the format string that
- consume arguments, then an error is generated.
- .PP
- A similar example as with fBbinary formatfR should explain the
- relation between field specifiers and arguments in case of the binary
- scan subcommand:
- .CS
- fBbinary scan $bytes s3s first secondfR
- .CE
- .PP
- This command (provided the binary string in the variable fIbytesfR
- is long enough) assigns a list of three integers to the variable
- fIfirstfR and assigns a single value to the variable fIsecondfR.
- If fIbytesfR contains fewer than 8 bytes (i.e. four 2-byte
- integers), no assignment to fIsecondfR will be made, and if
- fIbytesfR contains fewer than 6 bytes (i.e. three 2-byte integers),
- no assignment to fIfirstfR will be made. Hence:
- .CS
- fBputs [binary scan abcdefg s3s first second]fR
- fBputs $firstfR
- fBputs $secondfR
- .CE
- will print (assuming neither variable is set previously):
- .CS
- fB1fR
- fB25185 25699 26213fR
- fIcan't read "second": no such variablefR
- .CE
- .PP
- It is fBimportantfR to note that the fBcfR, fBsfR, and fBSfR
- (and fBifR and fBIfR on 64bit systems) will be scanned into
- long data size values. In doing this, values that have their high
- bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints),
- will be sign extended. Thus the following will occur:
- .CS
- fBset signShort [binary format s1 0x8000]fR
- fBbinary scan $signShort s1 val; fI# val == 0xFFFF8000fR
- .CE
- 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:
- .CS
- fBset val [expr {$val & 0xFFFF}]; fI# val == 0x8000fR
- .CE
- .PP
- 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:
- .IP fBafR 5
- The data is a character string of length fIcountfR. If fIcountfR
- is fB*fR, then all of the remaining bytes in fIstringfR will be
- scanned into the variable. If fIcountfR is omitted, then one
- character will be scanned.
- All characters scanned will be interpreted as being in the
- range \u0000-\u00ff so the fBencoding convertfromfR command might be
- needed if the string is not an ISO 8859-1 string.
- For example,
- .RS
- .CS
- fBbinary scan abcde\000fghi a6a10 var1 var2fR
- .CE
- will return fB1fR with the string equivalent to fBabcde\000fR
- stored in fBvar1fR and fBvar2fR left unmodified.
- .RE
- .IP fBAfR 5
- This form is the same as fBafR, except trailing blanks and nulls are stripped from
- the scanned value before it is stored in the variable. For example,
- .RS
- .CS
- fBbinary scan "abc efghi \000" A* var1fR
- .CE
- will return fB1fR with fBabc efghifR stored in fBvar1fR.
- .RE
- .IP fBbfR 5
- The data is turned into a string of fIcountfR binary digits in
- low-to-high order represented as a sequence of ``1'' and ``0''
- characters. The data bytes are scanned in first to last order with
- the bits being taken in low-to-high order within each byte. Any extra
- bits in the last byte are ignored. If fIcountfR is fB*fR, then
- all of the remaining bits in fBstringfR will be scanned. If
- fIcountfR is omitted, then one bit will be scanned. For example,
- .RS
- .CS
- fBbinary scan \x07\x87\x05 b5b* var1 var2fR
- .CE
- will return fB2fR with fB11100fR stored in fBvar1fR and
- fB1110000110100000fR stored in fBvar2fR.
- .RE
- .IP fBBfR 5
- This form is the same as fBbfR, except the bits are taken in
- high-to-low order within each byte. For example,
- .RS
- .CS
- fBbinary scan \x70\x87\x05 B5B* var1 var2fR
- .CE
- will return fB2fR with fB01110fR stored in fBvar1fR and
- fB1000011100000101fR stored in fBvar2fR.
- .RE
- .IP fBhfR 5
- The data is turned into a string of fIcountfR 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 fIcountfR
- is fB*fR, then all of the remaining hex digits in fBstringfR will be
- scanned. If fIcountfR is omitted, then one hex digit will be
- scanned. For example,
- .RS
- .CS
- fBbinary scan \x07\x86\x05 h3h* var1 var2fR
- .CE
- will return fB2fR with fB706fR stored in fBvar1fR and
- fB50fR stored in fBvar2fR.
- .RE
- .IP fBHfR 5
- This form is the same as fBhfR, except the digits are taken in
- high-to-low order within each byte. For example,
- .RS
- .CS
- fBbinary scan \x07\x86\x05 H3H* var1 var2fR
- .CE
- will return fB2fR with fB078fR stored in fBvar1fR and
- fB05fR stored in fBvar2fR.
- .RE
- .IP fBcfR 5
- The data is turned into fIcountfR 8-bit signed integers and stored
- in the corresponding variable as a list. If fIcountfR is fB*fR,
- then all of the remaining bytes in fBstringfR will be scanned. If
- fIcountfR is omitted, then one 8-bit integer will be scanned. For
- example,
- .RS
- .CS
- fBbinary scan \x07\x86\x05 c2c* var1 var2fR
- .CE
- will return fB2fR with fB7 -122fR stored in fBvar1fR and fB5fR
- stored in fBvar2fR. Note that the integers returned are signed, but
- they can be converted to unsigned 8-bit quantities using an expression
- like:
- .CS
- fBexpr { $num & 0xff }fR
- .CE
- .RE
- .IP fBsfR 5
- The data is interpreted as fIcountfR 16-bit signed integers
- represented in little-endian byte order. The integers are stored in
- the corresponding variable as a list. If fIcountfR is fB*fR, then
- all of the remaining bytes in fBstringfR will be scanned. If
- fIcountfR is omitted, then one 16-bit integer will be scanned. For
- example,
- .RS
- .CS
- fBbinary scan \x05\x00\x07\x00\xf0\xff s2s* var1 var2fR
- .CE
- will return fB2fR with fB5 7fR stored in fBvar1fR and fB-16fR
- stored in fBvar2fR. Note that the integers returned are signed, but
- they can be converted to unsigned 16-bit quantities using an expression
- like:
- .CS
- fBexpr { $num & 0xffff }fR
- .CE
- .RE
- .IP fBSfR 5
- This form is the same as fBsfR except that the data is interpreted
- as fIcountfR 16-bit signed integers represented in big-endian byte
- order. For example,
- .RS
- .CS
- fBbinary scan \x00\x05\x00\x07\xff\xf0 S2S* var1 var2fR
- .CE
- will return fB2fR with fB5 7fR stored in fBvar1fR and fB-16fR
- stored in fBvar2fR.
- .RE
- .IP fBifR 5
- The data is interpreted as fIcountfR 32-bit signed integers
- represented in little-endian byte order. The integers are stored in
- the corresponding variable as a list. If fIcountfR is fB*fR, then
- all of the remaining bytes in fBstringfR will be scanned. If
- fIcountfR is omitted, then one 32-bit integer will be scanned. For
- example,
- .RS
- .CS
- fBbinary scan \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff i2i* var1 var2fR
- .CE
- will return fB2fR with fB5 7fR stored in fBvar1fR and fB-16fR
- stored in fBvar2fR. Note that the integers returned are signed, but
- they can be converted to unsigned 32-bit quantities using an expression
- like:
- .CS
- fBexpr { $num & 0xffffffff }fR
- .CE
- .RE
- .IP fBIfR 5
- This form is the same as fBIfR except that the data is interpreted
- as fIcountfR 32-bit signed integers represented in big-endian byte
- order. For example,
- .RS
- .CS
- fBbinary scan \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0 I2I* var1 var2fR
- .CE
- will return fB2fR with fB5 7fR stored in fBvar1fR and fB-16fR
- stored in fBvar2fR.
- .RE
- .IP fBwfR 5
- .VS 8.4
- The data is interpreted as fIcountfR 64-bit signed integers
- represented in little-endian byte order. The integers are stored in
- the corresponding variable as a list. If fIcountfR is fB*fR, then
- all of the remaining bytes in fBstringfR will be scanned. If
- fIcountfR is omitted, then one 64-bit integer will be scanned. For
- example,
- .RS
- .CS
- fBbinary scan \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff wi* var1 var2fR
- .CE
- will return fB2fR with fB30064771077fR stored in fBvar1fR and
- fB-16fR stored in fBvar2fR. Note that the integers returned are
- signed and cannot be represented by Tcl as unsigned values.
- .RE
- .IP fBWfR 5
- This form is the same as fBwfR except that the data is interpreted
- as fIcountfR 64-bit signed integers represented in big-endian byte
- order. For example,
- .RS
- .CS
- fBbinary scan \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0 WI* var1 var2fR
- .CE
- will return fB2fR with fB21474836487fR stored in fBvar1fR and fB-16fR
- stored in fBvar2fR.
- .VE
- .RE
- .IP fBffR 5
- The data is interpreted as fIcountfR 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
- fIcountfR is fB*fR, then all of the remaining bytes in
- fBstringfR will be scanned. If fIcountfR is omitted, then one
- single-precision floating point number will be scanned. The size of a
- floating point number may vary across architectures, so the number of
- bytes that are scanned may vary. If the data does not represent a
- valid floating point number, the resulting value is undefined and
- compiler dependent. For example, on a Windows system running on an
- Intel Pentium processor,
- .RS
- .CS
- fBbinary scan \x3f\xcc\xcc\xcd f var1fR
- .CE
- will return fB1fR with fB1.6000000238418579fR stored in
- fBvar1fR.
- .RE
- .IP fBdfR 5
- This form is the same as fBffR except that the data is interpreted
- as fIcountfR double-precision floating point numbers in the
- machine's native representation. For example, on a Windows system
- running on an Intel Pentium processor,
- .RS
- .CS
- fBbinary scan \x9a\x99\x99\x99\x99\x99\xf9\x3f d var1fR
- .CE
- will return fB1fR with fB1.6000000000000001fR
- stored in fBvar1fR.
- .RE
- .IP fBxfR 5
- Moves the cursor forward fIcountfR bytes in fIstringfR. If
- fIcountfR is fB*fR or is larger than the number of bytes after the
- current cursor cursor position, then the cursor is positioned after
- the last byte in fIstringfR. If fIcountfR is omitted, then the
- cursor is moved forward one byte. Note that this type does not
- consume an argument. For example,
- .RS
- .CS
- fBbinary scan \x01\x02\x03\x04 x2H* var1fR
- .CE
- will return fB1fR with fB0304fR stored in fBvar1fR.
- .RE
- .IP fBXfR 5
- Moves the cursor back fIcountfR bytes in fIstringfR. If
- fIcountfR is fB*fR or is larger than the current cursor position,
- then the cursor is positioned at location 0 so that the next byte
- scanned will be the first byte in fIstringfR. If fIcountfR
- is omitted then the cursor is moved back one byte. Note that this
- type does not consume an argument. For example,
- .RS
- .CS
- fBbinary scan \x01\x02\x03\x04 c2XH* var1 var2fR
- .CE
- will return fB2fR with fB1 2fR stored in fBvar1fR and fB020304fR
- stored in fBvar2fR.
- .RE
- .IP fB@fR 5
- Moves the cursor to the absolute location in the data string specified
- by fIcountfR. Note that position 0 refers to the first byte in
- fIstringfR. If fIcountfR refers to a position beyond the end of
- fIstringfR, then the cursor is positioned after the last byte. If
- fIcountfR is omitted, then an error will be generated. For example,
- .RS
- .CS
- fBbinary scan \x01\x02\x03\x04 c2@1H* var1 var2fR
- .CE
- will return fB2fR with fB1 2fR stored in fBvar1fR and fB020304fR
- stored in fBvar2fR.
- .RE
- .SH "PLATFORM ISSUES"
- Sometimes it is desirable to format or scan integer values in the
- native byte order for the machine. Refer to the fBbyteOrderfR
- element of the fBtcl_platformfR array to decide which type character
- to use when formatting or scanning integers.
- .SH EXAMPLES
- This is a procedure to write a Tcl string to a binary-encoded channel as
- UTF-8 data preceded by a length word:
- .CS
- proc writeString {channel string} {
- set data [encoding convertto utf-8 $string]
- puts -nonewline [fBbinary formatfR Ia* e
- [string length $data] $data]
- }
- .CE
- .PP
- This procedure reads a string from a channel that was written by the
- previously presented fBwriteStringfR procedure:
- .CS
- proc readString {channel} {
- if {![fBbinary scanfR [read $channel 4] I length]} {
- error "missing length"
- }
- set data [read $channel $length]
- return [encoding convertfrom utf-8 $data]
- }
- .CE
- .SH "SEE ALSO"
- format(n), scan(n), tclvars(n)
- .SH KEYWORDS
- binary, format, scan