problem with bytearray conversion

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: problem with bytearray conversion

Post by dhylands » Mon Jan 04, 2016 6:56 pm

I've updated dump_mem.py so that it uses hexlify on micropython and regular hex formatting under cpython (since cpython's hexlify doesn't have the notion of seperators). https://github.com/dhylands/bioloid3/bl ... ump_mem.py It's a bit more code, but it uses memory much more effectively.

I also made log= be an argument rather than an imported module which makes dump_mem.py standalone.

After running dump_mem_test.py from the same directory, with the old version , the micropython heap looked like this:

Code: Select all

stack: 416 out of 15360
GC: total: 102080, used: 17616, free: 84464
 No. of 1-blocks: 560, 2-blocks: 67, max blk sz: 80
GC memory layout; from 20003140:
03140: hthhhhhhhthBhMDhhhhhhhtttttttttttttttttttttttttttttttttttttttttt
03540: ttttttttttttttttttttttttttttttttttttthhhBhhBtthhthtttttttttttttt
03940: ttttttttttthhhhhhhttttttthttttt....hhhtLhthhhhhhhhhhhhhhhhhhhhht
03d40: hhthhhtttttttththtththhthhtthhtthhtthhtthhtthhhhhttttttthhthttth
04140: thhhhhhththhtttthtttthhhhhhhhhhhhhhthhthhhhhhhhtttttttttthhhthht
04540: hhtthhhhhhhhhhhhhhhhhhhhthhhhhhthhhhhhthhhhhhthhhhhhthhhhhhhtttt
04940: tttttttttttthtthhhhhtthhhhhhtthhhhhhtthhhhhhtthhhhhhttthhthhhhth
04d40: htttthhttttthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhthhhhhhthhhhhhthhh
05140: hhhthhhhhhthhhhhhtthhhhhhtthhhhhhtthhhhhhtthhhhhhtthhhhhhttthhth
05540: hhhthhtttthhttttthhhhhhhhhhhhhhhhhhhhhhthhthhthhthhthhtthhtthhtt
05940: hhtthhtthhttthhhhthhtttthhtttthhhhhhhhhhhhhhhhhhhhhhhhhhhhthhthh
05d40: thhthhthhtthhtthhtthhtthhtthhttthhhhthhtttthhtttthhhhhhhhhhhhhhh
06140: hhhhhhhhhhhhhhhhhhhthhhhhhthhhhhhthhhhhhthhtthhttthhhhhhhhhhhhhh
06540: hhhhhhhhhhhhhhhhhhthhhhhhthhhhhhthhhhhhthhtthhttthhhhhhhhhhhhhhh
06940: hhhhhhhthhthhthhhhthhtthhtth........hhhhhhhhhhhhhhhhhhhhhhhhhhhh
06d40: hhhthhhhhhthhhhthhthhthhtthhtthhtthhtthhtthhttthhhhthhtttthhtttt
07140: hhhhhhhhhhhhhhhhhhhhhhhhhhthhthhthhthhthhtthhtthhtthhtthhtthhttt
07540: hhhhhhttthhttthhttttttttt.......................................
       (81 lines all free)
1bd40: ............................................
and with the new version it looks like:

Code: Select all

stack: 416 out of 15360
GC: total: 102080, used: 10512, free: 91568
 No. of 1-blocks: 307, 2-blocks: 15, max blk sz: 80
GC memory layout; from 20003140:
03140: hthhhhhhhthBhMDhhhhhhhtttttttttttttttttttttttttttttttttttttttttt
03540: ttttttttttttttttttttttttttttttttttttthhhBBhhhhhhhhtttttttttttttt
03940: ttttttttttthhhhhhhttttttthttttthtttBtthhhh....hhhhhLhthhhhhhhttt
03d40: tthhhhhhtthhhhhhhttttttthhhhhttttthhhhhhhttttthhhttttttthhhhhhhh
04140: hhhhhhhththhtthhhhhhttttthhhhhhhhhhthhthhhhhhhhttttthhhhhhhhthht
04540: hhtthhhhhhhhhhtthhhthhhhhttttthhhhhhhhhhhtthhhhhhhhhhhhhhtttttth
04940: httttthhhhtttttttttthttttthhhhhhhhhhhhhhhhhhtthhhhhhhhhhttttthhh
04d40: htththtthhhhhttthhhhhhhhhhhhhhhthhhhhhhttthhhhhhhhthhhhhhhttthhh
05140: hhhhhhhhhhhhhhhhhhttth.........hhhttttthhhhhhhhhhhhhhhthhhthhhht
05540: tttttttttttttttttttttttttttthhhhhhhttttthhhhhtttthhhhhhhhhhhhhhh
05940: hhhtthhhhhhhhhhtttthhttttttttt..................................
       (88 lines all free)
1bd40: ............................................
which is about 7K less heap usage.

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: problem with bytearray conversion

Post by EasyRider » Mon Jan 04, 2016 9:39 pm

Once again Thank you all.

Code: Select all

print(binascii.hexlify(bytearray(message), b":")) 
message being the integer list/array.

Does the trick nicely in converting and formatted printing of integer list/array > bytearray/hex string bytes.

PythonNoobie
Posts: 1
Joined: Wed Jan 08, 2020 5:00 pm

Re: problem with bytearray conversion

Post by PythonNoobie » Wed Jan 22, 2020 7:49 pm

I realize this post is now over 3 years old, but something that helped me that I did not realize at first;

If I am not mistaken, pythons default output is in Unicode.

Unicode uses all the same characters that hexadecimal does, but also many more.

I kept thinking that my output was wrong because it looked like some bytes were okay and some were not.

What I didn't think about is: what is pythons defaults format is none is specified.


After adding .hex to ----> = ser.read(8).hex() compared to just writing ser.read(8). This fixed my problems.

Doesn't work on micropython for uart.read() though. This is a problem I am facing now!

You could try to store the initial reading into a variable and then convert later.

Always think about what format you may be in when not specifying one. Hope this helps someone.

Post Reply