如何从Tcl中的十六进制值字符串中提取位字段并将其输出为整数



我有表示十六进制值的字符串,其中包含多个不同位长的字段。我必须把它们提取出来并打印在屏幕上。例如0x17AF018D1是33比特的十六进制值,其中比特被索引为0到32;我需要排除位0-5、6-7、8-21、22-30、31-32中包含的数字。

我相信有多种方法可以做到这一点。实现这种双脚的最佳方法是什么?

十六进制值可以直接作为整数处理。Tcl的整数实际上是任意精度值,但经过优化,可以有效地与主机系统的机器字大小配合使用。(Tcl为您透明地处理细节。(

这意味着比特字段提取器可以是这样的(假设小端序(:

proc BitField {value from to} {
if {$from > $to} {
error "reversed bit field description"
}
# How many bits wide is the field?
set width [expr {$to - $from + 1}]
# Convert the width into a bit mask in the lowest bits
set mask [expr {(1 << $width) - 1}]
# Shift and mask the value to get the field
expr {($value >> $from) & $mask}
}
set abc 0x17AF018D1
puts [BitField $abc 0 5]
puts [BitField $abc 6 7]
puts [BitField $abc 8 21]
puts [BitField $abc 22 30]
# You are aware this overlaps?
puts [BitField $abc 30 32]

对于不重叠的连续字段,您可以这样做:

# Note that this is big endian as it is working with the string representation
scan [format "%033lb" $abc] "%3b%8b%14b%2b%6b" e d c b a
puts $a
puts $b
puts $c
puts $d
puts $e

字符串中的值是总值/字段宽度:%033lb表示格式化为33位二进制值(在您的示例中为101111010111100000001100011010001(,%3b表示此时解析3位二进制值。(不幸的是,我们无法提高可读性,因为scan说明符之间不能有空格,因为我们刚刚生成的输入数据中没有空格。(

最新更新