📄 binary.n
字号:
.CS\fBbinary format a5@2a1@*a3@10a1 abcde f ghi j\fR.CEwill return \fBabfdeghi\\000\\000j\fR..RE.SH "BINARY SCAN".PPThe \fBbinary scan\fR command parses fields from a binary string,returning the number of conversions performed. \fIString\fR gives theinput to be parsed and \fIformatString\fR indicates how to parse it.Each \fIvarName\fR gives the name of a variable; when a field isscanned from \fIstring\fR the result is assigned to the correspondingvariable..PPAs with \fBbinary format\fR, the \fIformatString\fR consists of asequence of zero or more field specifiers separated by zero or morespaces. Each field specifier is a single type character followed byan optional numeric \fIcount\fR. Most field specifiers consume oneargument to obtain the variable into which the scanned values shouldbe placed. The type character specifies how the binary data is to beinterpreted. The \fIcount\fR typically indicates how many items ofthe specified type are taken from the data. If present, the\fIcount\fR is a non-negative decimal integer or \fB*\fR, whichnormally indicates that all of the remaining items in the data are tobe used. If there are not enough bytes left after the current cursorposition to satisfy the current field specifier, then thecorresponding variable is left untouched and \fBbinary scan\fR returnsimmediately with the number of variables that were set. If there arenot enough arguments for all of the fields in the format string thatconsume arguments, then an error is generated..PPEach type-count pair moves an imaginary cursor through the binary data,reading bytes from the current position. The cursor is initiallyat position 0 at the beginning of the data. The type may be any one ofthe following characters:.IP \fBa\fR 5The data is a character string of length \fIcount\fR. If \fIcount\fRis \fB*\fR, then all of the remaining bytes in \fIstring\fR will bescanned into the variable. If \fIcount\fR is omitted, then onecharacter will be scanned. For example,.RS.CS\fBbinary scan abcde\\000fghi a6a10 var1 var2\fR.CEwill return \fB1\fR with the string equivalent to \fBabcde\\000\fRstored in \fBvar1\fR and \fBvar2\fR left unmodified..RE.IP \fBA\fR 5This form is the same as \fBa\fR, except trailing blanks and nulls are stripped fromthe scanned value before it is stored in the variable. For example,.RS.CS\fBbinary scan "abc efghi \\000" a* var1\fR.CEwill return \fB1\fR with \fBabc efghi\fR stored in \fBvar1\fR..RE.IP \fBb\fR 5The data is turned into a string of \fIcount\fR binary digits inlow-to-high order represented as a sequence of ``1'' and ``0''characters. The data bytes are scanned in first to last order withthe bits being taken in low-to-high order within each byte. Any extrabits in the last byte are ignored. If \fIcount\fR is \fB*\fR, thenall of the remaining bits in \fBstring\fR will be scanned. If\fIcount\fR is omitted, then one bit will be scanned. For example,.RS.CS\fBbinary scan \\x07\\x87\\x05 b5b* var1 var2\fR.CEwill return \fB2\fR with \fB11100\fR stored in \fBvar1\fR and\fB1110000110100000\fR stored in \fBvar2\fR..RE.IP \fBB\fR 5This form is the same as \fBB\fR, except the bits are taken inhigh-to-low order within each byte. For example,.RS.CS\fBbinary scan \\x70\\x87\\x05 b5b* var1 var2\fR.CEwill return \fB2\fR with \fB01110\fR stored in \fBvar1\fR and\fB1000011100000101\fR stored in \fBvar2\fR..RE.IP \fBh\fR 5The data is turned into a string of \fIcount\fR hexadecimal digits inlow-to-high order represented as a sequence of characters in the set``0123456789abcdef''. The data bytes are scanned in first to lastorder with the hex digits being taken in low-to-high order within eachbyte. Any extra bits in the last byte are ignored. If \fIcount\fRis \fB*\fR, then all of the remaining hex digits in \fBstring\fR will bescanned. If \fIcount\fR is omitted, then one hex digit will bescanned. For example,.RS.CS\fBbinary scan \\x07\\x86\\x05 h3h* var1 var2\fR.CEwill return \fB2\fR with \fB706\fR stored in \fBvar1\fR and\fB50\fR stored in \fBvar2\fR..RE.IP \fBH\fR 5This form is the same as \fBh\fR, except the digits are taken inlow-to-high order within each byte. For example,.RS.CS\fBbinary scan \\x07\\x86\\x05 H3H* var1 var2\fR.CEwill return \fB2\fR with \fB078\fR stored in \fBvar1\fR and\fB05\fR stored in \fBvar2\fR..RE.IP \fBc\fR 5The data is turned into \fIcount\fR 8-bit signed integers and storedin the corresponding variable as a list. If \fIcount\fR is \fB*\fR,then all of the remaining bytes in \fBstring\fR will be scanned. If\fIcount\fR is omitted, then one 8-bit integer will be scanned. Forexample,.RS.CS\fBbinary scan \\x07\\x86\\x05 c2c* var1 var2\fR.CEwill return \fB2\fR with \fB7 -122\fR stored in \fBvar1\fR and \fB5\fRstored in \fBvar2\fR. Note that the integers returned are signed, butthey can be converted to unsigned 8-bit quantities using an expressionlike:.CS\fBexpr ( $num + 0x100 ) % 0x100\fR.CE.RE.IP \fBs\fR 5The data is interpreted as \fIcount\fR 16-bit signed integersrepresented in little-endian byte order. The integers are stored inthe corresponding variable as a list. If \fIcount\fR is \fB*\fR, thenall of the remaining bytes in \fBstring\fR will be scanned. If\fIcount\fR is omitted, then one 16-bit integer will be scanned. Forexample,.RS.CS\fBbinary scan \\x05\\x00\\x07\\x00\\xf0\\xff s2s* var1 var2\fR.CEwill return \fB2\fR with \fB5 7\fR stored in \fBvar1\fR and \fB-16\fRstored in \fBvar2\fR. Note that the integers returned are signed, butthey can be converted to unsigned 16-bit quantities using an expressionlike:.CS\fBexpr ( $num + 0x10000 ) % 0x10000\fR.CE.RE.IP \fBS\fR 5This form is the same as \fBs\fR except that the data is interpretedas \fIcount\fR 16-bit signed integers represented in big-endian byteorder. For example,.RS.CS\fBbinary scan \\x00\\x05\\x00\\x07\\xff\\xf0 S2S* var1 var2\fR.CEwill return \fB2\fR with \fB5 7\fR stored in \fBvar1\fR and \fB-16\fRstored in \fBvar2\fR. .RE.IP \fBi\fR 5The data is interpreted as \fIcount\fR 32-bit signed integersrepresented in little-endian byte order. The integers are stored inthe corresponding variable as a list. If \fIcount\fR is \fB*\fR, thenall of the remaining bytes in \fBstring\fR will be scanned. If\fIcount\fR is omitted, then one 32-bit integer will be scanned. Forexample,.RS.CS\fBbinary scan \\x05\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\xf0\\xff\\xff\\xff i2i* var1 var2\fR.CEwill return \fB2\fR with \fB5 7\fR stored in \fBvar1\fR and \fB-16\fRstored in \fBvar2\fR. Note that the integers returned are signed andcannot be represented by Tcl as unsigned values..RE.IP \fBI\fR 5This form is the same as \fBI\fR except that the data is interpretedas \fIcount\fR 32-bit signed integers represented in big-endian byteorder. For example,.RS.CS\fBbinary \\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x07\\xff\\xff\\xff\\xf0 I2I* var1 var2\fR.CEwill return \fB2\fR with \fB5 7\fR stored in \fBvar1\fR and \fB-16\fRstored in \fBvar2\fR..RE.IP \fBf\fR 5The data is interpreted as \fIcount\fR single-precision floating pointnumbers in the machine's native representation. The floating pointnumbers are stored in the corresponding variable as a list. If\fIcount\fR is \fB*\fR, then all of the remaining bytes in\fBstring\fR will be scanned. If \fIcount\fR is omitted, then onesingle-precision floating point number will be scanned. The size of afloating point number may vary across architectures, so the number ofbytes that are scanned may vary. If the data does not represent avalid floating point number, the resulting value is undefined andcompiler dependent. For example, on a Windows system running on anIntel Pentium processor,.RS.CS\fBbinary scan \\x3f\\xcc\\xcc\\xcd f var1\fR.CEwill return \fB1\fR with \fB1.6000000238418579\fR stored in\fBvar1\fR..RE.IP \fBd\fR 5This form is the same as \fBf\fR except that the data is interpretedas \fIcount\fR double-precision floating point numbers in themachine's native representation. For example, on a Windows systemrunning on an Intel Pentium processor,.RS.CS\fBbinary scan \\x9a\\x99\\x99\\x99\\x99\\x99\\xf9\\x3f d var1\fR.CEwill return \fB1\fR with \fB1.6000000000000001\fRstored in \fBvar1\fR..RE.IP \fBx\fR 5Moves the cursor forward \fIcount\fR bytes in \fIstring\fR. If\fIcount\fR is \fB*\fR or is larger than the number of bytes after thecurrent cursor cursor position, then the cursor is positioned afterthe last byte in \fIstring\fR. If \fIcount\fR is omitted, then thecursor is moved forward one byte. Note that this type does notconsume an argument. For example,.RS.CS\fBbinary scan \\x01\\x02\\x03\\x04 x2H* var1\fR.CEwill return \fB1\fR with \fB0304\fR stored in \fBvar1\fR..RE.IP \fBX\fR 5Moves the cursor back \fIcount\fR bytes in \fIstring\fR. If\fIcount\fR is \fB*\fR or is larger than the current cursor position,then the cursor is positioned at location 0 so that the next bytescanned will be the first byte in \fIstring\fR. If \fIcount\fRis omitted then the cursor is moved back one byte. Note that thistype does not consume an argument. For example,.RS.CS\fBbinary scan \\x01\\x02\\x03\\x04 c2XH* var1 var2\fR.CEwill return \fB2\fR with \fB1 2\fR stored in \fBvar1\fR and \fB020304\fRstored in \fBvar2\fR..RE.IP \fB@\fR 5Moves the cursor to the absolute location in the data string specifiedby \fIcount\fR. Note that position 0 refers to the first byte in\fIstring\fR. If \fIcount\fR refers to a position beyond the end of\fIstring\fR, then the cursor is positioned after the last byte. If\fIcount\fR is omitted, then an error will be generated. For example,.RS.CS\fBbinary scan \\x01\\x02\\x03\\x04 c2@1H* var1 var2\fR.CEwill return \fB2\fR with \fB1 2\fR stored in \fBvar1\fR and \fB020304\fRstored in \fBvar2\fR..RE.SH "PLATFORM ISSUES"Sometimes it is desirable to format or scan integer values in thenative byte order for the machine. Refer to the \fBbyteOrder\fRelement of the \fBtcl_platform\fR array to decide which type characterto use when formatting or scanning integers..SH "SEE ALSO"format, scan, tclvars.SH KEYWORDSbinary, format, scan
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -