一个把十六进制数转换为十进制数的小程序: riple
proc h2d {{hex_num 0}} {
set tmp1 0x
append tmp1 $hex_num
set tmp2 [format "%d" $tmp1]
return $tmp2
}
一个读取二进制文件的小程序: riple
# Show current directory
set tmp [pwd]
puts "\n Current dir : $tmp"
# Open file
puts "\n Type in the file name"
gets stdin disk_file_name
set disk_file_fileid [open "$disk_file_name" "r"]
fconfigure $disk_file_fileid -translation binary
# Main loop
while {1} {
# Set read original address
puts "\n Type in the addrByte"
gets stdin addrByte
seek $disk_file_fileid $addrByte start
# read binary
puts "\n Type in the numByte"
gets stdin numByte
set disk_read [read $disk_file_fileid $numByte]
binary scan $disk_read "H*" tmp0
puts "\nReturned [expr [string bytelength $tmp0] / 2] Byte(s) : "
puts "$tmp0"
puts "\n"
}
# Close file
close $disk_file_fileid
一个顺序执行初始化、写入、读出的交互式创建文件的小程序 riple
proc wr_file {{file_id} {Byte_content 5A} {Byte_num 512}} {
set loop_end $Byte_num
set loop_num 0
while {$loop_num < $loop_end} {
puts -nonewline $file_id $Byte_content
set loop_num [expr $loop_num + 1]
}
flush $file_id
}
proc rd_file {{file_id} {Byte_num 512}} {
return [read $file_id [expr $Byte_num * 2]]
}
# show current directory
set tmp [pwd]
puts "\n Current dir : $tmp"
# open file
file mkdir disk_sector
puts "\n Type in the file name"
gets stdin disk_file_name
set disk_fileid [open "disk_sector/$disk_file_name" "w+"]
fconfigure $disk_fileid -translation binary
# init file
puts "\n INIT FILE"
puts "\n Type in the Byte content"
gets stdin Byte_content
puts "\n Type in the Byte number"
gets stdin Byte_num
wr_file $disk_fileid $Byte_content $Byte_num
# write file
puts "\n WRITE FILE"
puts "\n Type in the Byte content"
gets stdin Byte_content
puts "\n Type in the Byte address"
gets stdin Byte_addr
puts "\n Type in the Byte number"
gets stdin Byte_num
seek $disk_fileid [expr $Byte_addr * 2] start
wr_file $disk_fileid $Byte_content $Byte_num
# read file
puts "\n READ FILE"
puts "\n Type in the Byte address"
gets stdin Byte_addr
puts "\n Type in the Byte number"
gets stdin Byte_num
seek $disk_fileid [expr $Byte_addr * 2] start
puts [rd_file $disk_fileid $Byte_num]
# close file
close $disk_fileid
文件访问权限: riple
r+ :可取可存;文件必须已经存在。 riple
w+ :先存后取;文件存在会被清空,文件不存在会被创建。 riple
标签: TCL
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.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论