[英]What is the Python equivalent of CPP reinterpret_cast
I got stuck in a trivial problem of reinterpret_cast
casting operator. 我陷入了
reinterpret_cast
cast操作员的一个小问题。 Basically, in CPP, I have a float
variable which is used to create a uint32_t
variable using reinterpret_cast
as shown below- 基本上,在CPP中,我有一个
float
变量,用于使用reinterpret_cast
创建一个uint32_t
变量,如下所示 -
float x = 2.2949836e-38;
uint32_t rgb = *reinterpret_cast<uint32_t*>(&x);
printf("rgb=%d", rgb); // prints rgb=16377550
I want to achieve the same in python. 我想在python中实现相同的功能。 Please note that conventional int casting isn't producing the expected result.
请注意,传统的int铸造不会产生预期的结果。
You can use pack
, unpack
from struct
module: 您可以使用
pack
,从struct
module unpack
:
from struct import pack, unpack
b = pack('f', 2.2949836e-38)
print(unpack('i', b)[0])
Prints: 打印:
16377550
Edit: 编辑:
shortened example 缩短的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.