Python: Decoding byte array

Viewed 42

can someone tell me how to decode this bytearray:

bytearray(b"~QF\xc2\x13\x04\xca=`\xc3\xef\xfd\x8bc\xb3\xcf\'V\xa3n\xeb\xccX\x97\xb2\xe6\xe0\xca\x12\x85\xac\x8b\xca\xd3hj\xb3\xc0\xedF\x84\x91\x83\t\xfe:\xdd@\xeb?O\xf8\xc2\xf1,\x8d\xfc\x1ag\xddZ\xd0\x14\xcc")

I've tried everything I can think of. Different ways of value.decode() where I used different encodings (utf-8, utf-16-le, windows-1252, ascii, the list goes on) and base64 encodings (last I tried was base64.b64decode since it truncates the padding).

I've an example of the decoding in an NodeJS application: Buffer.from(bluetoothBarrier.getReceivedMessage()).toString('base64')

But I don't know how to replicate this in Python...

1 Answers
Buffer.from(bluetoothBarrier.getReceivedMessage()).toString('base64')

This appears to be converting the received message into a base64 string.

If that's all you want to do the correct implementation would be:

base64_bytes = base64.b64encode(bytesarray)
base64_string = base64_bytes.decode('utf-8')

This produces base64 encode string:

'flFGwhMEyj1gw+/9i2OzzydWo27rzFiXsubgyhKFrIvK02hqs8DtRoSRgwn+Ot1A6z9P+MLxLI38GmfdWtAUzA=='
Related