pointer typecasting like c in python

故事扮演 提交于 2019-12-11 08:06:32

问题


I've a c code to type cast a string to an integer via pointer.

char s[]="efgh";
int * p;
p=(int *) s;
printf("%d",*p);

This gives me an output of:

1751606885

Which is a 32 bit integer.

I'm analyzing a network packet in python and need the above functionality in python. I've a string

s="efgh"

and want the above in a 32 bit integer (from the byte level).

How can I do it?


回答1:


You can try struct.unpack:

>>> import struct
>>>
>>> struct.unpack('<I', 'efgh')
(1751606885,)
>>>


来源:https://stackoverflow.com/questions/23155437/pointer-typecasting-like-c-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!