user3615194
user3615194

Reputation: 31

How do I convert my byte data to integer using python?

I have a byte data is like: b'\xc4\x03\x00\x00\xe2\xecqv\x01'.
How do I convert this to integer like index by index.

Upvotes: 3

Views: 53

Answers (3)

JZkea
JZkea

Reputation: 1

If you have Python v.3, you can use the int_from_bytes() function:

int.from_bytes(b'\xc4\x03\x00\x00\xe2\xecqv\x01', byteorder='big') 

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46921

a bytes object is basically already a (immutable) sequence of integers.

b = b'\xc4\x03\x00\x00\xe2\xecqv\x01'
b[0] # 196
lst = list(b)
#  [196, 3, 0, 0, 226, 236, 113, 118, 1]

Upvotes: 2

Netwave
Netwave

Reputation: 42796

Just access throug indexes:

>>> b = b'\xc4\x03\x00\x00\xe2\xecqv\x01'
>>> b[0]
196

>>> for i in b:
...     print(i)
... 
196
3
0
0
226
236
113
118
1

Upvotes: 0

Related Questions