1

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 Answer 1

8

You can try struct.unpack:

>>> import struct
>>>
>>> struct.unpack('<I', 'efgh')
(1751606885,)
>>>
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any option to get that value in unsigned int. for my network analysis implementation, I'm getting a negative number. But i need an unsigned one. and can anyone tell me what is happening here?
@RatDon Use I instead of i, read about two's complement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.