binary----由二进制字符串中插入,提取字段
语法:
binary format formatString ?arg arg ...?
binary scan string formatString ?varName varName ...?
该命令为操作二进制数据提供了手段。
第一种方式,binary format,通过一般的tcl值创建二进制字符串。比如,在32-bit平台上,给定两个值16和22,使用该命令可以产生一个由两个4字节的整形数组成的8字节的二进制字符串。
第二种方式,binary scan,作相反操作:其将从二进制字符串中除去出来数据并以一般的tcl字符串值返回
binary的转换类型:
a
A character string of length count. Padded with nulls in binary format.
binary format a7a5a alpha bravo charlie
====>alpha\000\000bravoc
binary scan abcde\000fghi a6a10 var1 var2
====>will return 1 with the string equivalent to abcde\000 stored in var1 and var2 left unmodified.
A
A character string of length count. Padded with spaces in binary format. Trailing nulls and blanks are discarded in binary scan.
binary format A6A*A alpha bravo charlie
====>alpha bravoc
binary scan "abc efghi \000" A* var1
====>will return 1 with abc efghi stored in var1.
b
A binary string of length count. Low-to-high order.
binary format b5b* 11100 111000011010
====>\x07\x87\x05
binary format b* 11100000
binary scan \x07\x87\x05 b5b* var1 var2
will return 2 with 11100 stored in var1 and 1110000110100000 stored in var2.
B
A binary string of length count. High-to-low order.
binary format B5B* 11100 111000011010
====>\xe0\xe1\xa0
binary scan \x70\x87\x05 B5B* var1 var2
will return 2 with 01110 stored in var1 and 1000011100000101 stored in var2.
h
A hexadecimal string of length count. Low-to-high order.
binary format h3h* AB def
====>\xba\x00\xed\x0f
binary scan \x07\x86\x05 h3h* var1 var2
will return 2 with 706 stored in var1 and 50 stored in var2.
H
A hexadecimal string of length count. High-to-low order. (More commonly used than h.)
binary format H3H* ab DEF
====>\xab\x00\xde\xf0
binary scan \x07\x86\x05 H3H* var1 var2
will return 2 with 078 stored in var1 and 05 stored in var2.
c
An 8-bit character code. The count is for repetition.
binary format c3cc* {3 -3 128 1} 260 {2 5}
====>\x03\xfd\x80\x04\x02\x05
binary format c {2 5}
====>提示错误
binary scan \x07\x86\x05 c2c* var1 var2
will return 2 with 7 -122 stored in var1 and 5 stored in var2. Note that the integers returned are signed, but they can be converted to unsigned 8-bit quantities using an expression like:
expr { $num & 0xff }
s
A 16-bit integer in little-endian byte order. The count is for repetition.
binary format s3 {3 -3 258 1}
====>\x03\x00\xfd\xff\x02\x01
binary scan \x05\x00\x07\x00\xf0\xff s2s* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2. Note that the integers returned are signed, but they can be converted to unsigned 16-bit quantities using an expression like:
expr { $num & 0xffff }
S
A 16-bit integer in big-endian byte order. The count is for repetition.
binary format S3 {3 -3 258 1}
====>\x00\x03\xff\xfd\x01\x02
binary scan \x00\x05\x00\x07\xff\xf0 S2S* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2.
i
A 32-bit integer in little-endian byte order. The count is for repetition.
binary format i3 {3 -3 65536 1}
====>\x03\x00\x00\x00\xfd\xff\xff\xff\x00\x00\x01\x00
binary scan \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff i2i* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2. Note that the integers returned are signed, but they can be converted to unsigned 32-bit quantities using an expression like:
expr { $num & 0xffffffff }
I
A 32-bit integer in big-endian byte order. The count is for repetition.
binary format I3 {3 -3 65536 1}
====>\x00\x00\x00\x03\xff\xff\xff\xfd\x00\x01\x00\x00
binary scan \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0 I2I* var1 var2
will return 2 with 5 7 stored in var1 and -16 stored in var2.
w
A 64-bit integer in little-endian byte order. The count is for repetition. (Tcl 8.4)
binary format w 7810179016327718216
====>HelloTcl
binary scan \x05\x00\x00\x00\x07\x00\x00\x00\xf0\xff\xff\xff wi* var1 var2
will return 2 with 30064771077 stored in var1 and -16 stored in var2. Note that the integers returned are signed and cannot be represented by Tcl as unsigned values.
W
A 64-bit integer in big-endian byte order. The count is for repetition. (Tcl 8.4)
binary format Wc 4785469626960341345 110
====>BigEndian
binary scan \x00\x00\x00\x05\x00\x00\x00\x07\xff\xff\xff\xf0 WI* var1 var2
will return 2 with 21474836487 stored in var1 and -16 stored in var2.
f
Single-precision floating point value in native format.The count is for repetition.
binary format f2 {1.6 3.4}
====>\xcd\xcc\xcc\x3f\x9a\x99\x59\x40
binary scan \x3f\xcc\xcc\xcd f var1
will return 1 with 1.6000000238418579 stored in var1.
d
Double-precision floating point value in native format. The count is for repetition.
binary format d1 {1.6}
====>\x9a\x99\x99\x99\x99\x99\xf9\x3f
binary scan \x9a\x99\x99\x99\x99\x99\xf9\x3f d var1
will return 1 with 1.6000000000000001 stored in var1.
x
Pack count null bytes with binary format.
binary format a3xa3x2a3 abc def ghi
====>abc\000def\000\000ghi
Skip count bytes with binary scan.
binary scan \x01\x02\x03\x04 x2H* var1
will return 1 with 0304 stored in var1.
X
Backup count bytes.
binary format a3X*a3X2a3 abc def ghi
====>dghi
binary scan \x01\x02\x03\x04 c2XH* var1 var2
will return 2 with 1 2 stored in var1 and 020304 stored in var2.
@
Skip to absolute position specified by count. If count is *, skip to the end.
binary format a5@2a1@*a3@10a1 abcde f ghi j
====>abfdeghi\000\000j
binary scan \x01\x02\x03\x04 c2@1H* var1 var2
will return 2 with 1 2 stored in var1 and 020304 stored in var2.
举例:
This is a procedure to write a Tcl string to a binary-encoded channel as UTF-8 data preceded by a length word:
proc writeString {channel string} {
set data [encoding convertto utf-8 $string]
puts -nonewline [binary format Ia* \
[string length $data] $data]
}
This procedure reads a string from a channel that was written by the previously presented writeString procedure:
proc readString {channel} {
if {![binary scan [read $channel 4] I length]} {
error "missing length"
}
set data [read $channel $length]
return [encoding convertfrom utf-8 $data]
}
阅读(3036) | 评论(0) | 转发(0) |