- Raspberry Pi-Pico connected to Windows 10 64 bit
- Coding in MicroPython 3.7.9 (32-bit) via Thonny 3.3.3
I am trying to get a binary string of 0s & 1s into a byte array.
If I convert them into a character first it’s okay, that is until the values go over 127d (8 bits) then I get this:
Code: Select all
num8Bits = ['01111110',
'11000011',
'11000011',
'11000011',
'01111110',
'01111110',
'11000011',
'11000011',
'11000011',
'11000011',
'01111110']
y=''
for x in num8Bits:
y += chr(int(x,2))
mybytearray = bytearray(y)
print (mybytearray)
Expected >>bytearray(b'\x7e\xc3\xc3\xc3\x7e\x7e\xc3\xc3\xc3\xc3\x7e')
Result >>bytearray(b'~\xc3\x83\xc3\x83\xc3\x83~~\xc3\x83\xc3\x83\xc3\x83\xc3\x83~')
I have also tried building the string as a hex set of values as opposed to characters, these assemble as bytes such as 0x7e0xc30xc30xc30x7e…. and so on
But if I try the recommend method of something like bytes.fromhex(‘ 7ec3c3c37e’) I get : File AttributeError: type object 'bytes' has no attribute 'fromhex'
Is this a limitation of MicroPython over python I wonder?
Any pointers would be most helpful.
Cheers