1

How can I convert object of class 'bytes'

b'\xd2\x9f\r'

To string of '0' and '1'

001011010110000010010

2 Answers 2

2

Use the code below

import sys
bin(int.from_bytes(b'\xd2\x9f\r', byteorder=sys.byteorder))

output 0b11011001111111010010

Sign up to request clarification or add additional context in comments.

Comments

1

In Micropython on ESP32 was the result:

>>> bin(int.from_bytes(b'-\x05\x05\xff',byteorder=sys.byteorder))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function doesn't take keyword arguments

Replacing the byteorder param with a default 0 did the job:

>>> bin(int.from_bytes(b'-\x05\x05\xff',0))
'0b101101000001010000010111111111'

Comments

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.