3

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.

1
  • It depends on how you want to interpret that as integers. You can take each byte and convert it to integer, or each bit, or each 16-bit word or interpret the whole data as one integer, or anything else. In the end it sums up to question: how did you get that binary data in the first place? Commented Mar 25, 2019 at 7:31

3 Answers 3

2

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.