Page 1 of 1

Convert Byte-String to Ascii-String -SOLVED

Posted: Mon Jul 25, 2022 2:59 pm
by MMliam
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

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

Posted: Mon Jul 25, 2022 5:39 pm
by MMliam
Found the solution, to my own question :D

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

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

Posted: Mon Jul 25, 2022 10:12 pm
by Jibun no kage
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?

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

Posted: Tue Jul 26, 2022 12:34 pm
by MMliam
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'

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

Posted: Tue Jul 26, 2022 1:42 pm
by karfas
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.

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

Posted: Tue Jul 26, 2022 1:59 pm
by MMliam
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.

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

Posted: Tue Jul 26, 2022 2:22 pm
by scruss
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'

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

Posted: Tue Jul 26, 2022 5:43 pm
by MMliam
I totally agree, binascii (Python), or ubinascii (microPython) is always necessary for these conversions.

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

Posted: Sat Jul 30, 2022 3:47 am
by dhylands
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.