For context; I have an arduino project code from last semester and have been starting to explore python. I extracted the code from the arduino and now I’m trying to convert the hex code from the arduino into ascii so I can just have my code back.
This isn’t going to be elegant and I’ve only got some experience with C++.
And the code that I’m translating will look something along the lines of:
:20 00E0 00 AC35B107D9F722E0ACE5B1E001C01D92A433B207E1F710E0C2E6D0E004C02197 D7
On notepad++ the 20 is the start code, the 00E0 is the address, and the last 00 is brown(record type?), then the D7 is the byte count. The big string in the middle is black. I’m assuming that everything except the big long string can be discarded since I pulled this from my arduino memory.
But again I’m new and doing this to get a feel for how to figure crap out on my own in a way that’s not graded.
Thanks for reading.
First; my understanding of how this should work/the playbook I’ve followed so far:
- Open the file with the hex code in it
- Read the file with the hex code into a temp file
- Convert the text in the temp file from hex to binary to ascii, or just hex to ascii
- Write the lines in the temp file into the actual output file
Now my code:
from math import ceil
import binascii
import tempfile
import os
# open the thing
f = open(" ", "r")
# read the thing
with open(os.path.abspath(f.name), 'r') as file:
lines = file.readlines()
hexLines = []
for item in lines:
hexLines.append(item.encode('utf-8').hex)
binascii.unhexlify(hexLines)
# convert the thing
with tempfile.NamedTemporaryFile() as temp:
print(hexLines)
# Write thing
g = open(" ", "w")
g.write('r')
TheMil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.