我有一个 c 代码,可以通过指针将字符串类型转换为整数。
char s[]="efgh";
int * p;
p=(int *) s;
printf("%d",*p);
这给了我一个输出:
1751606885
这是一个 32 位整数。
我正在用 python 分析网络数据包,需要在 python 中使用上述功能。 我有一个字符串
s="efgh"
并希望以上内容为 32 位整数(从字节级别)。
我该怎么做?
最佳答案
你可以试试struct.unpack
:
>>> import struct
>>>
>>> struct.unpack('<I', 'efgh')
(1751606885,)
>>>
关于python - 像python中的c一样的指针类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155437/