TCL:变量存储浮点值,如何将其作为十六进制打印在屏幕上?
TCL: A variable stores a floating point value, how can this be printed on screen as a hex?
如何在 tcl 中存储浮点数的十六进制表示:
% set a 1.3
1.3
% puts $a
1.3
我想将十六进制表示存储在另一个变量中,然后将其打印在屏幕上或用于其他目的。
How do I store the hex representation of a floating point number in
Tcl:
您的 已得到解答,应该会为您指明正确的方向:将给定的浮点数转换为 32 位整数(这称为 IEEE 754 binary32 表示),然后将将此 binary32 表示转换为十六进制形式。例如,在 Tcl (8.6+) 中,您可以通过以下方式实现:
% set a 1.3
1.3
% set ahex [binary encode hex [binary format f $a]]; # hex will be in big-endian form!
6666a63f
根据前面的回答,您可以使用以下方法反向操作:
% binary scan [binary format i 0x$ahex] R x
1
% set x
1.2999999523162842
注意使用 R
说明符(大端)到 binary scan
,而不是 f
(小或大,平台相关),因为产生的十六进制是大端形式。一般来说,binary32转换要注意字节序
如何在 tcl 中存储浮点数的十六进制表示:
% set a 1.3
1.3
% puts $a
1.3
我想将十六进制表示存储在另一个变量中,然后将其打印在屏幕上或用于其他目的。
How do I store the hex representation of a floating point number in Tcl:
您的
% set a 1.3
1.3
% set ahex [binary encode hex [binary format f $a]]; # hex will be in big-endian form!
6666a63f
根据前面的回答,您可以使用以下方法反向操作:
% binary scan [binary format i 0x$ahex] R x
1
% set x
1.2999999523162842
注意使用 R
说明符(大端)到 binary scan
,而不是 f
(小或大,平台相关),因为产生的十六进制是大端形式。一般来说,binary32转换要注意字节序