指针typecasting像c在python [英] pointer typecasting like c in python

查看:280
本文介绍了指针typecasting像c在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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,)
>>>>


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?

解决方案

You can try struct.unpack:

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

这篇关于指针typecasting像c在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆