需求:
原list输出格式
0x00 0x50 0x01 0x03 0x04 0x02 0x21 0x57 0x01 0x00 0x05 0x0B 0x03 0x13 0x00 0x01
要求list输出格式为
list21 is : 04030150
list22 is : 01572102
list23 is : 030b0500
list24 is : 00010013
list20 is : 04030150 01572102 030b0500 00010013
思路
1. 去除0x方式显示;
2. 四字节连成一个string,string再转化成list;
3.不够四字节前面补零。
代码
#!/usr/bin/env tclsh
set mReply {0x00 0x50 0x01 0x03 0x04 0x02 0x21 0x57 0x01 0x00 0x05 0x0B 0x03 0x13 0x00 0x01}
set len [llength $mReply] for {set i 1} {$i<=[expr $len-1]} {incr i} {
set mBit [format "%02x" [lindex $mReply $i]]
set mReply [lreplace $mReply $i $i $mBit]
} set lenTemp [expr {$len-1}]
while {1} {
set lenFlag [expr {$lenTemp%4}]
if {$lenFlag!=0} {
set mReply [linsert $mReply end 00]
incr len
set lenTemp [expr {$len-1}]
} else {
break;
}
} set flag 0
for {set j 1} {$j<=[expr {$len-1}]} {set j [expr {$j+4}]} {
incr flag
for {set k 0} {$k<=3} {incr k} {
set list2$flag [list [append var$flag [lindex $mReply [expr {$flag*4-$k}]]]]
}
}
puts "list21 is : $list21"
puts "list22 is : $list22"
puts "list23 is : $list23"
puts "list24 is : $list24" set list20 [concat $list21 $list22 $list23 $list24]
puts "list20 is : $list20"
不得不感叹,tcl对于处理字符串和list来说真的是太方便了!继续加油!