学习Tcl(三)——字符串数值转换
0赞进行string和对应数值之间转换的小程序
set numeric_type [lindex $argv 0]
puts "\noriginal numbr in hex : $numeric_type"
set string_type [binary format "H2" $numeric_type]
puts "transformed to string : $string_type"
binary scan $string_type "H*" numeric_again
puts "transformed to number : $numeric_again"
set string_again [binary format "H2" $numeric_again]
puts "transformed to string : $string_again\n"
进行长串数值和string之间转换的小程序
puts "\n"
set hex_num [lrange $argv 0 end]
puts "number inputed : $hex_num"
set hex_len [expr [string length $hex_num]/2]
puts "number length : $hex_len"
for {set i 0} {$i < $hex_len} {incr i 1} {
set hex_byte [string range $hex_num [expr $i*2] [expr $i*2+1]]
puts "number [expr $i+1]: $hex_byte"
append str_ing [binary format "H2" $hex_byte]
puts "string appended : $str_ing"
}
puts "\n"