文档详情

Tcl中binary format 和binary scan命令分析.doc

发布:2017-07-17约3.62千字共3页下载文档
文本预览下载声明
Tcl中binary format 和binary scan命令分析 Tcl中,用binary format 和binary scan来处理二进制文件用得比较多,但这个两个命令比较难理解。我花了一天的时间,终于略知一二。现和大家分享下。 一:binary命令的解释 binary format binary scan ? 帮助给出的解释: This command provides facilities for manipulating binary data. The first form, binary format, 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, binary scan, does the opposite: it extracts data from a binary string and returns it as ordinary Tcl string values. 大意是:该命令是对二进制数据进行操作。binary format命令,是把普通的Tcl 数据转换成二进制字符,例如:在32位的机器上,可以把16和22这样的数据,转换成由两个4字节的整数组成的8字节的二进制字符串(一个二进制字符的显示图形,是由字符编码方式决定的,在记事本里有ANSI、Unicode、Unicode big endian和UTF-8编码方式,关于字符编码可看:字符编码笔记:ASCII,Unicode和UTF-8)。binary scan命令,功能正好与binary format命令相反,是把二进制字符转换成正常的Tcl 数据。 ? 二:binary命令的语法 1.binary format formatString ?arg arg ...? The binary format command generates a binary string whose layout is specified by the formatString and whose contents come from the additional arguments. The resulting binary value is returned binary format命令接收数据(arg arg ...?)并根据模板(formatString)进行压缩转换,最后返回转换的值。 处理不同的数据用不同的模板,比如待处理的数据是二进制数(例:1001010)可用b或B,待处理的数据是十六进制数(例:FF)可用h或H;并根据待处理数据的长度,设置count,比如待处理二进制数1001010长为8,则count=8,(在其它模板中,count还可表示重复特征等)。 ? 2.binary scan string formatString ?varName varName ...? The binary scan command parses fields from a binary string, returning the number of conversions performed. String gives the input bytes to be parsed (one byte per character, and characters not representable as a byte have their high bits chopped) and formatString indicates how to parse it. Each varName gives the name of a variable; when a field is scanned from string the result is assigned to the corresponding variable. Binary scan 命令根据模块(formatString)从一二进制字符里解析获得一数值,并把该数值赋给变量(arg arg ...?),该命令返回解析的字符的个数。 待解析的字符可能是由几个字节组成,到底由几个字节组成一个字符,由通道的属性来决定,比如fconfigure channel
显示全部
相似文档