Startec
Startec

Reputation: 13246

Python 3: How to convert a bytearray to an ASCII string

I have the following bytearray

bytearray(b'S\x00t\x00a\x00n\x00d\x00a\x00r\x00d\x00F\x00i\x00r\x00m\x00a\x00t\x00a\x00.\x00i\x00n\x00o\x00')

It should spell out StandardFirmata.ino however, I can't figure out how to decode it.

Here is what I have tried:

print(str(board.sysex_list)) #Appears to just return a string that looks identical
print(board.sysex_list.decode()) # Returns just S

Is there a simple way to do this?

Upvotes: 0

Views: 20301

Answers (2)

Startec
Startec

Reputation: 13246

The issue was that I was not specifying a decoding. All I had to do was change decode to decode('utf-16-le')

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799490

Wrong encoding.

3>> bytearray(b'S\x00t\x00a\x00n\x00d\x00a\x00r\x00d\x00F\x00i\x00r\x00m\x00a\x00t\x00a\x00.\x00i\x00n\x00o\x00').decode('utf-16le')
'StandardFirmata.ino'

But that's not ASCII.

Upvotes: 7

Related Questions