Ok, I have been doing some research and have hit a bit of a wall when it comes to my understanding:
You need
Two Hex char to byte and byte to two hex char
Four Hex char to int and int to two hex char
You can cheat and build the four hex out of two hex by calling hex_to_byte two times.
From what I can see this is not really necessary, as far as I know I can only send the hex file over the serial port using ASCII, so I can convert these characters to bytes very easily by simply casting them as a byte.
Will try to translate above to help you understand this.
If you look at the hardware for a async serial port you will see a setting for how many data bits(5 - 8 ) & with each setting, I have sent the equal of Intel Hex format text over that link.
The 5 data bit format was mostly used by a TTY. The character put on paper was determined by the order that characters were put in a location in the type box & the labels on the keys the humans pressed.
You have the machine that did the work with a very small human part the labels and type hammer. The machine only used a few "format effectors" or "control characters" ā the CR (Carriage Return) and LF (Line Feed) codes.
Today you are dealing with what is the machine and what is the human standard. The machine part has got smarter and can shift between different human standards.
You have a binary machine that works with bits & arrays of bits. And in some cases can have a binary interpretation to the machine.
A bit or array of bits can and do have many human interpretations.
If I have a binary pressure value, a binary temperature value, a binary voltage value all with the same number of bits, your casting would turn one of these to another type of value with the bits the same. This would be like 45 psi = 105 F = 11 Volts. Some programming languages lets you use programmer named types and does a lot of error checking to find programming errors like this.
So a character IS NOT a byte but could have same storage space.
Most terminal programs that you can run on a PC have the ability to capture the text sent to the PC and to send a text file. Not needing to write special program on PC side has big benefits. You can change the Arduino side an not have to make a change on PC side program. And later you can still add a PC side program if you want.
In addition you could have the capability of just using batch program files to work the programmer.
What I have found out is that when using the Serial Port with windows the data is sent in Unicode, not in ASCII, which would explain the problems I wByte data array to Byte data arrayas having earlier with the python script that was running on the computer side. My school technician showed me that you need to convert the Unicode to ASCII before you send it over serial when using python.
Why?
What is preventing Arduino from working with both except the programmer writing the code?
Today you only have a few different character encoding the Arduino would need to work with and only a very few characters for each encoding.
How could you bypass the need to know the value of a character and allow also for the many different encodings?
For the conversion of hex to a binary value you only need a few, what if you put the characters in an array?
Hex_Ascii = "0123456789ABCDEF"
The array location of the match tells you what is needed in the Hex to binary function, the actual character value is unimportant.
You could have a second array for Unicode
Hex_Unicode= "0123456789ABCDEF"
Now the worst case is that you need a function to fill in the array values for the encodings that the programming language does not understand.
The nice thing here is the arrays can also be used in the from binary to hex function.
Using a two dimension array will make the code easer to read.
Now you could start off with the dumb mode programming that works and later may be able to auto detect ASCII vs Unicode.
You have heard this
"Press 1 for English", "Press 2 for Spanish"
Dumb mode programming that works!
If you have looked at Intel HEX format you only need one more character, the ":". Put this character in a second array with others you may need in remaining parts of program.
Now I would suggest that you start a new program that in the future you will copy functions from to add to your current program.
Have the PC send one line of Intel Hex to Arduino and work on what is needed.
You should note that Intel Hex has a checksum to check for transmission errors. This suggests that you should receive one line and then check if valid before translating from HEX to Binary.
To allow for a simple PC copy file to serial port the ":" should be a menu choice that jumps to Intel Hex receive function.
https://en.wikipedia.org/wiki/Intel_HEXDo #2 while putting received characters in a character array,
Byte count, two hex digits, indicating the number of bytes (hex digit pairs) in the data field. The maximum byte count is 255 (0xFF). 16 (0x10) and 32 (0x20) are commonly used byte counts.
To determine how many more characters to read for a complete line.
When you have received the proper number of characters,
1. Do a checksum test ( note this could be a dummy function to start)
2. Convert from HEX to binary. Here the converted results would be best to have in an array. Many reasons for this. While testing you could have a function that reads the array and outputs to serial port Intel HEX. This function with little to no changes then lets you output the contents of a Rom/memory to serial port.
So a list of functions
Two characters in HEX to BYTE (two hex digits)
BYTE to Two characters in HEX
You need a (four hex digits) versions
Serial Receive Intel Hex Line to character array
Intel Hex formated character array to serial send
Intel HEX Line character array to byte data array
Byte data array to Intel HEX line character Arduino
Byte data array to ROM/Memory
ROM/Memory to Byte data array
Byte data array to Byte data array Match check.
Functions for testing
character array to character array
Byte data array to Byte data array
To make these functions easy, use a two dimension array so you can keep the data separate when needed.
So simple test and get it working code
Receive Intel Hex line, Write Intel Hex Line back.
As you get each function working add more of the lower level functions and test again.
When you get to point of adding the write/read to rom stop and start sending more Intel Hex lines of data.
You need to remember that you have a rate that you can send or receive with a serial port. To be able to receive at full rate you need to send the same or less characters back or you will have a backup characters to send. If you do not do this then you need to tell the PC to stop sending or you have a PC program that waits for a response from Arduino.
Arduino has limited RAM space and RAM arrays can use up memory fast. Try to have two locations for each chunk of data such that the only common data location is the actual attached ROM/ram. If PC to rom uses one array index and rom to PC uses a different index, then the program just got better and also easer to test.