Convert Byte-String to Ascii-String -SOLVED

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Convert Byte-String to Ascii-String -SOLVED

Post by MMliam » Mon Jul 25, 2022 2:59 pm

Steps that convert a byte-string to an ascii-string:
>>> print(Message)
b'4f6e'
>>> x=Message.decode('ascii')
>>> print(x)
4f6e
>>> y=ubinascii.unhexlify(x)
>>> print(y)
b'On'
>>> z=y.decode('ascii')
>>> print(z)
On
Is there a simpler, one step process to make the same conversion?
In other words turning this: b'4f6e' into this: On
Last edited by MMliam on Mon Jul 25, 2022 5:45 pm, edited 1 time in total.

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Convert Byte-String to Ascii-String - SOLVED

Post by MMliam » Mon Jul 25, 2022 5:39 pm

Found the solution, to my own question :D

message=b'4f6e'
message=ubinascii.unhexlify(message).decode('ascii')
print(message)
On

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Convert Byte-String to Ascii-String -SOLVED

Post by Jibun no kage » Mon Jul 25, 2022 10:12 pm

I found this works... str(theByteString, 'utf-8') as well. Does the job of converting b'' string decoding. Maybe a good thing to avoid another import unless you really need it?

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Convert Byte-String to Ascii-String -SOLVED

Post by MMliam » Tue Jul 26, 2022 12:34 pm

I tried it in PYTHON Idle & micropython.
msg = b'4f6e'
msg = str(msg, 'utf-8')
print(msg)
4f6e
It did not decode the byte stream to - On

Tried:
msg = b'4f6e'
msg = str(msg, 'utf-8').decode('ascii')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
msg = str(msg, 'utf-8').decode('ascii')
AttributeError: 'str' object has no attribute 'decode'.
BTW: 'ascii' works as well as 'utf-8'

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Convert Byte-String to Ascii-String -SOLVED

Post by karfas » Tue Jul 26, 2022 1:42 pm

In python 3 works

Code: Select all

>>> msg = b'4f6e'
>>> result = str(binascii.unhexlify(msg), 'ascii')
>>> result
'On'
>>> type(result)
<class 'str'>
See documentation for unhexlify and str.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Convert Byte-String to Ascii-String -SOLVED

Post by MMliam » Tue Jul 26, 2022 1:59 pm

Jibun no kage wrote:
Mon Jul 25, 2022 10:12 pm
I found this works... str(theByteString, 'utf-8') as well. Does the job of converting b'' string decoding. Maybe a good thing to avoid another import unless you really need it?
The 'Jibun no kage' suggestion was to avoid importing "binascii"; your approach and mine require importing "binascii' for Python, or "ubinascii" for micropython.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Convert Byte-String to Ascii-String -SOLVED

Post by scruss » Tue Jul 26, 2022 2:22 pm

You'll always need (u)binascii for this kind of data, as it's a series of hexadecimal digits encoded as ASCII bytes. If it were bytes, the input would be b'On'

MMliam
Posts: 121
Joined: Mon May 07, 2018 1:08 pm

Re: Convert Byte-String to Ascii-String -SOLVED

Post by MMliam » Tue Jul 26, 2022 5:43 pm

I totally agree, binascii (Python), or ubinascii (microPython) is always necessary for these conversions.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Convert Byte-String to Ascii-String -SOLVED

Post by dhylands » Sat Jul 30, 2022 3:47 am

I also have a little utilty I wrote called dump_mem.py which you can find here:
https://github.com/dhylands/bioloid3/bl ... ump_mem.py

It prints out output that looks like this:

Code: Select all

0000: 30 31 32 33 34 35 36 37 01234567
0008: 38 39 41 42 43 44 45 46 89ABCDEF
0010: 47                      G
It has options for specifying line length, adding a Prefix to each line, whether the ASCII should be shown along side the hex or not. whether the address should be shown, and what function to use to send the output to.

Post Reply