一个把十六进制数转换为十进制数的小程序: 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
用户377235 2014-6-7 09:46
binary scan $disk_read "H*" tmp0 这个是把disk_read中的每个1或0转换为ACSII码,然后以16进制的字符串显示出来,不是把一个二进制字符串转换为对应的16进制字符串,上面的写法有问题吧!