jim jammere
jim jammere

Reputation: 13

taking one byte from bytearray python

how can I take one of this byte from bytearray (I need it in the from of byte not an integer)

bytearray(b'\x01\x02\x87\x0b\x1e\x9e\xc9\xde\xb7\n+\x92\n\x03\t')

Upvotes: 0

Views: 1049

Answers (1)

planetmaker
planetmaker

Reputation: 6044

It's a matter of appropriately formatting the output - the internal value is not changed:

x = bytearray(b'\x01\x02\x87\x0b\x1e\x9e\xc9\xde\xb7\n+\x92\n\x03\t')
print("{:02x}".format(x[0]))

gives correctly:

01

If you absolutely need to retain the bytes property, go for something like

y = bytes([x[5]])
print(y)

which will then give you

b'\x9e'

Upvotes: 2

Related Questions