Go Down

Topic: Convert .HEX file to a .BIN file (Read 362 times) previous topic - next topic

RayPerry1

I archived a 24LC256 EEProm on an SD card.  I have a 64 KB file with the 2 character Hex values.

Converting the binary bytes to Hex was a breeze.  Serial.Print(n, HEX) did that for me.

Now, I need to re-create the EEProm.

My question:  How do I convert a 2 character hex value to single byte Binary values to put back onto an EEProm?

I'm sure it's already answered in this forum but, my old eyes are intimidated by 173 pages of QnA.

Any help would be great.  Thanks

GolamMostafa

#1
May 10, 2019, 01:44 pm Last Edit: May 10, 2019, 01:45 pm by GolamMostafa
My question:  How do I convert a 2 character hex value to single byte Binary values to put back onto an EEProm?
That means  that you have 2-byte ASCII coded data, and you want to convert/transform them into a 1- byte binary coded data. For example: 0x31 (0011 0001)  and 0x42 (0100 0010) are ASCII coded data for the  characters/digits 1  and B respectifully. Now, you want to manipulate these data to get 1B (0001 1011). Is my thesis correct?

RayPerry1

I want to read in "31" from my HEX SD file and write  this 8 bit value (0011 0001) one location on my EEProm.

Thanks,

GolamMostafa

#3
May 10, 2019, 03:54 pm Last Edit: May 10, 2019, 03:55 pm by GolamMostafa
I want to read in "31" from my HEX SD file and write  this 8 bit value (0011 0001) one location on my EEProm.
So far I know that the SD Card stores data in ASCII format. EEPROM location holds data in binary format. Assume that a location of the EEPROM contains 0x31; how are you going to save it in the SD card? Of course, 0x31 would be saved as 0x33 and 0x31 -- the ASCII codes of 3 and 1.

If you read back the data from the SD card, you will receive 0x33 (0011 0011) and 0x31 (0011 0001). In order to put the original data (0x31) into the EEPROM, you have to combine 0x33 and 0x31 to form 0x31. To do so, the codes are:

Code: [Select]
byte x1 = 0x33;
byte x2 = 0x31;
byte x = (x1 - 0x30 <<4) | (x2 - 0x30)  //x = 31


If x1/x2 ranges from 0x00 - 0xFF, then we have use a different program to extract the binary byte to accommodate digits A - F.

RayPerry1

Thank you very much for your help.
I'm going to read in the ascii values of the HEX file as type BYTE
Subtract 48
if the result is greater than 9 then subtract another 7.
Shift the nibble 4 bits left and add the results of the second character's processing.

for example:
Read in the first two bytes.     "A5"   (Text)
The two binary values are        65  Ascii "A"
                                           53  Ascii "5"

65-48=17  Sub another 7 = 1010 = upper nibble
53-48=5 = 0101 = lower nibble

The resulting byte is 10100101

Thanks for your help

Reading the data in as byte values solves my problem.

GolamMostafa

Excellent effort to devise the algorithm for accommodating the digits: 0 - F. Can we see the codes that you are creating for this algorithm?

christop

Unfortunately, if all you did to write hex was Serial.Print(n, HEX) without a separator or some way to ensure that each value is two digits long, then you have no way to properly decode your data!

Suppose you have the three values 01 AB 05, and they're written to the file as "1AB5". Is that supposed to be 1A B5, or 01 0A B5, or 1A 0B 05, or some other combination of values?

GolamMostafa

#7
May 11, 2019, 07:29 am Last Edit: May 11, 2019, 10:36 am by GolamMostafa
Suppose you have the three values 01 AB 05, and they're written to the file as "1AB5". Is that supposed to be 1A B5, or 01 0A B5, or 1A 0B 05, or some other combination of values?
The binary formatted 3-byte data (01 AB 05) will be written into SD card as ASCII codes which are: 30, 31, 41, 42, 30, 45 (all are in hex base).

Lucario448

Just wondering: assuming you know how to split a byte in a "hex" (and pressumably ASCII/text-encoded) file, wouldn't be easier to just do this?:

Code: [Select]
byte val = atoi(hexASCIIValue, 16);
Where hexASCIIValue is a C-style string (aka char array) containing the two characters (nibbles) that make up the corresponding byte.

sterretje

The binary formatted 3-byte data (01 AB 05) will be written into SD card as ASCII codes which are: 30, 31, 41, 42, 30, 45 (all are in hex base).
Not when using the standard print functions because they omit leading zeroes. 0x01 will be written as character '1', not as characters '0' and '1'.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up