Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print 4 bytes as an integer

Tags:

python

I have an array of 4 bytes that I need to print it's value as an Integer. This is very easy in C, as I can cast and print with something like this:

 printf("Integer Value = %i", (int) pointer_to_4_bytes_array);

Thanks.

like image 773
OSM10 Avatar asked Oct 27 '25 09:10

OSM10


1 Answers

In Python2 (works on 3 too), you can use the struct module.

>>> import struct
>>> struct.unpack(">L","1234")[0]
825373492

In Python3.2+, int.from_bytes does what you want. See @ryrich's answer

>>> int.from_bytes(b"1234",'big')
825373492
like image 93
John La Rooy Avatar answered Oct 28 '25 23:10

John La Rooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!