将二进制数据变量转换为字节列表

Convert binary data variable to the list of bytes

我有从文件 set data [read $fp 5] 中读取的二进制数据变量。假设它是:0102030405 十六进制表示。

如何将它转换为这样的字节列表: {0x01 0x02 0x03 0x04 0x05}?

我有一个需要二进制数据作为列表的函数。我想 binary scan 是我需要的,但不知道如何使用它。

你是对的; binary scan 是您需要的工具。你可能想这样做:

binary scan $data "cu*" byteValues

byteValues 中生成无符号字节值列表。 (b 转换产生位串,这不是您想要的。)例如:

% binary scan Abc "cu*" theValues
1
% puts $theValues
65 98 99

如果你处理的是旧版本的 Tcl,你需要 c* 而不是 cu* 然后每个字节的最高位是符号位。结果技术上是正确的,但不是我发现非常有用的东西。