Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

MicroPython • micropython "lossy" bytes to string conversion

$
0
0
I'm currently doing a project with micropython and the pico display pack 2. I'm using pimironi's build of micropython to get support for the display pack, I think it's fairly recent but it may not be the absoloute latest.

I have some bytes, purportedly containing an error message. They should be in UTF-8 (indeed they will probablly be ascii) but there may have been corruption on a serial line, or just a programming bug which lead to other byte values getting in there. I want to convert them to a string in a way that won't fail (this is already an error handling path..........).

Normally in python you would do this with something like.

bytes = b'\xff'
bytes.decode('utf-8','replace')

Unfortunately I have discovered that in micropython this doesn't work. The parameters to bytes.decode seem to simply be ignored. The bytes are treated as utf-8 regardless of which encoding I speify and if they are not valid UTF-8 I get an exceptio instead of the replacement I asked for. So-far the only way I've found to convert bytes to string "safely" is repr, but that does a bunch of extra escaping which is undesirable in the normal case.

Currently I'm looking at the least bad option being to convert the string with bytes.decode and if that raises an exception convert them with repr.

Questions.

1, is this consistent across all micropython builds/variants? or is it some kind of build option I could possiblly chane if I rebuilt micropython?
2. Is there a better option I am missing?

Statistics: Posted by plugwash — Fri Mar 15, 2024 11:14 am



Viewing all articles
Browse latest Browse all 8041

Trending Articles