alcoder
alcoder

Reputation: 457

Convert <class 'bytes'> to string of '0' and '1' python

How can I convert object of class 'bytes'

b'\xd2\x9f\r'

To string of '0' and '1'

001011010110000010010

Upvotes: 1

Views: 444

Answers (2)

Mark SdS
Mark SdS

Reputation: 119

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'

Upvotes: 1

Siddharth Das
Siddharth Das

Reputation: 1115

Use the code below

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

output 0b11011001111111010010

Upvotes: 2

Related Questions