Skip to main content
 首页 » 编程设计

python之像python中的c一样的指针类型转换

2025年05月04日11bhlsheji

我有一个 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,) 
>>>