📄 binary.n
字号:
'\"'\" 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.'\" '\" SCCS: @(#) binary.n 1.7 97/11/11 19:08:47'\" .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 NAMEbinary \- 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.PPThis command provides facilities for manipulating binary data. Thefirst form, \fBbinary format\fR, creates a binary string from normalTcl values. For example, given the values 16 and 22, it might producean 8-byte binary string consisting of two 4-byte integers, one foreach of the numbers. The second form of the command, \fBbinary scan\fR, does the opposite: it extracts data from a binarystring and returns it as ordinary Tcl string values..SH "BINARY FORMAT".PPThe \fBbinary format\fR command generates a binary string whose layoutis specified by the \fIformatString\fR and whose contents come fromthe additional arguments. The resulting binary value is returned..PPThe \fIformatString\fR consists of a sequence of zero or more fieldspecifiers separated by zero or more spaces. Each field specifier isa single type character followed by an optional numeric \fIcount\fR.Most field specifiers consume one argument to obtain the value to beformatted. The type character specifies how the value is to beformatted. The \fIcount\fR typically indicates how many items of thespecified type are taken from the value. If present, the \fIcount\fRis a non-negative decimal integer or \fB*\fR, which normally indicatesthat all of the items in the value are to be used. If the number ofarguments does not match the number of fields in the format stringthat consume arguments, then an error is generated..PPEach type-count pair moves an imaginary cursor through the binarydata, storing bytes at the current position and advancing the cursorto just after the last byte stored. The cursor is initially atposition 0 at the beginning of the data. The type may be any one ofthe following characters:.IP \fBa\fR 5Stores a character string of length \fIcount\fR in the output string.If \fIarg\fR has fewer than \fIcount\fR bytes, then additional zerobytes are used to pad out the field. If \fIarg\fR is longer than thespecified length, the extra characters will be ignored. If\fIcount\fR is \fB*\fR, then all of the bytes in \fIarg\fR will beformatted. If \fIcount\fR is omitted, then one character will beformatted. For example,.RS.CS\fBbinary format a7a*a alpha bravo charlie\fR.CEwill return a string equivalent to \fBalpha\\000\\000bravoc\fR..RE.IP \fBA\fR 5This form is the same as \fBa\fR except that spaces are used forpadding instead of nulls. For example,.RS.CS\fBbinary format A6A*A alpha bravo charlie\fR.CEwill return \fBalpha bravoc\fR..RE.IP \fBb\fR 5Stores a string of \fIcount\fR binary digits in low-to-high orderwithin each byte in the output string. \fIArg\fR must contain asequence of \fB1\fR and \fB0\fR characters. The resulting bytes areemitted in first to last order with the bits being formatted inlow-to-high order within each byte. If \fIarg\fR has fewer than\fIcount\fR digits, then zeros will be used for the remaining bits.If \fIarg\fR has more than the specified number of digits, the extradigits will be ignored. If \fIcount\fR is \fB*\fR, then all of thedigits in \fIarg\fR will be formatted. If \fIcount\fR is omitted,then one digit will be formatted. If the number of bits formatteddoes not end at a byte boundary, the remaining bits of the last bytewill be zeros. For example,.RS.CS\fBbinary format b5b* 11100 111000011010\fR.CEwill return a string equivalent to \fB\\x07\\x87\\x05\fR..RE.IP \fBB\fR 5This form is the same as \fBb\fR except that the bits are stored inhigh-to-low order within each byte. For example,.RS.CS\fBbinary format B5B* 11100 111000011010\fR.CEwill return a string equivalent to \fB\\xe0\\xe1\\xa0\fR..RE.IP \fBh\fR 5Stores a string of \fIcount\fR hexadecimal digits in low-to-highwithin each byte in the output string. \fIArg\fR must contain asequence of characters in the set ``0123456789abcdefABCDEF''. Theresulting bytes are emitted in first to last order with the hex digitsbeing formatted in low-to-high order within each byte. If \fIarg\fRhas fewer than \fIcount\fR digits, then zeros will be used for theremaining digits. If \fIarg\fR has more than the specified number ofdigits, the extra digits will be ignored. If \fIcount\fR is\fB*\fR, then all of the digits in \fIarg\fR will be formatted. If\fIcount\fR is omitted, then one digit will be formatted. If thenumber of digits formatted does not end at a byte boundary, theremaining bits of the last byte will be zeros. For example,.RS.CS\fBbinary format h3h* AB def\fR.CEwill return a string equivalent to \fB\\xba\\xed\\x0f\fR..RE.IP \fBH\fR 5This form is the same as \fBh\fR except that the digits are stored inhigh-to-low order within each byte. For example,.RS.CS\fBbinary format H3H* ab DEF\fR.CEwill return a string equivalent to \fB\\xab\\xde\\xf0\fR..RE.IP \fBc\fR 5Stores one or more 8-bit integer values in the output string. If no\fIcount\fR is specified, then \fIarg\fR must consist of an integervalue; otherwise \fIarg\fR must consist of a list containing at least\fIcount\fR integer elements. The low-order 8 bits of each integerare stored as a one-byte value at the cursor position. If \fIcount\fRis \fB*\fR, then all of the integers in the list are formatted. Ifthe number of elements in the list is fewer than \fIcount\fR, then anerror is generated. If the number of elements in the list is greaterthan \fIcount\fR, then the extra elements are ignored. For example,.RS.CS\fBbinary format c3cc* {3 -3 128 1} 257 {2 5}\fR.CEwill return a string equivalent to\fB\\x03\\xfd\\x80\\x01\\x02\\x05\fR, whereas.CS\fBbinary format c {2 5}\fR.CEwill generate an error..RE.IP \fBs\fR 5This form is the same as \fBc\fR except that it stores one or more16-bit integers in little-endian byte order in the output string. Thelow-order 16-bits of each integer are stored as a two-byte value atthe cursor position with the least significant byte stored first. Forexample,.RS.CS\fBbinary format s3 {3 -3 258 1}\fR.CEwill return a string equivalent to \fB\\x03\\x00\\xfd\\xff\\x02\\x01\fR..RE.IP \fBS\fR 5This form is the same as \fBs\fR except that it stores one or more16-bit integers in big-endian byte order in the output string. Forexample,.RS.CS\fBbinary format S3 {3 -3 258 1}\fR.CEwill return a string equivalent to \fB\\x00\\x03\\xff\\xfd\\x01\\x02\fR..RE.IP \fBi\fR 5This form is the same as \fBc\fR except that it stores one or more32-bit integers in little-endian byte order in the output string. Thelow-order 32-bits of each integer are stored as a four-byte value atthe cursor position with the least significant byte stored first. Forexample,.RS.CS\fBbinary format i3 {3 -3 65536 1}\fR.CEwill return a string equivalent to \fB\\x03\\x00\\x00\\x00\\xfd\\xff\\xff\\xff\\x00\\x00\\x10\\x00\fR..RE.IP \fBI\fR 5This form is the same as \fBi\fR except that it stores one or more oneor 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.CEwill return a string equivalent to \fB\\x00\\x00\\x00\\x03\\xff\\xff\\xff\\xfd\\x00\\x10\\x00\\x00\fR..RE.IP \fBf\fR 5This form is the same as \fBc\fR except that it stores one or more oneor more single-precision floating in the machine's nativerepresentation in the output string. This representation is notportable across architectures, so it should not be used to communicatefloating point numbers across the network. The size of a floatingpoint number may vary across architectures, so the number of bytesthat are generated may vary. If the value overflows themachine's native representation, then the value of FLT_MAXas defined by the system will be used instead. Because Tcl usesdouble-precision floating-point numbers internally, there may be someloss 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.CEwill return a string equivalent to \fB\\xcd\\xcc\\xcc\\x3f\\x9a\\x99\\x59\\x40\fR..RE.IP \fBd\fR 5This form is the same as \fBf\fR except that it stores one or more oneor more double-precision floating in the machine's nativerepresentation in the output string. For example, on aWindows system running on an Intel Pentium processor,.RS.CS\fBbinary format d1 {1.6}\fR.CEwill return a string equivalent to \fB\\x9a\\x99\\x99\\x99\\x99\\x99\\xf9\\x3f\fR..RE.IP \fBx\fR 5Stores \fIcount\fR null bytes in the output string. If \fIcount\fR isnot specified, stores one null byte. If \fIcount\fR is \fB*\fR,generates an error. This type does not consume an argument. Forexample,.RS.CS\fBbinary format a3xa3x2a3 abc def ghi\fR.CEwill return a string equivalent to \fBabc\\000def\\000\\000ghi\fR..RE.IP \fBX\fR 5Moves the cursor back \fIcount\fR bytes in the output string. 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 bytestored will be the first byte in the result string. If \fIcount\fR isomitted then the cursor is moved back one byte. This type does notconsume an argument. For example,.RS.CS\fBbinary format a3X*a3X2a3 abc def ghi\fR.CEwill return \fBdghi\fR..RE.IP \fB@\fR 5Moves the cursor to the absolute location in the output stringspecified by \fIcount\fR. Position 0 refers to the first byte in theoutput string. If \fIcount\fR refers to a position beyond the lastbyte stored so far, then null bytes will be placed in the unitializedlocations and the cursor will be placed at the specified location. If\fIcount\fR is \fB*\fR, then the cursor is moved to the current end ofthe output string. If \fIcount\fR is omitted, then an error will begenerated. This type does not consume an argument. For example,.RS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -