tags:

views:

160

answers:

2

I need to read in an Intel Hex file which looks something like this:

:0300000002F8D42F
:07000300020096000000005E
:07000B000200B50000000037
:030013000200D414
:03001B000200F3ED  

(Yes, some lines are missing and sometimes 1 line only contains 1 byte)
The : is the start code
First 2 bytes is the byte count
Next 4 are the address in memory
Next 2 is record type
The rest is the data (except the last 2 bytes)
last 2 bytes are the checksum
More info here (wikipedia)

I need to end up with something like this (no periods, only there for readability):

:10.addr.RT.10bytesofdata.CK

If there is no data from the file for an address I am filling it with 'FF'

So what is the best way to read in and store a file like this if I am going to need to divide up and sort the information by address, byte for byte.
I was hoping to read byte by byte (?) storing the appropriate values into a 2D integer array ordered by the address.

[BC][ADDR][RT][b1][b2][b3][b4][b5][b6][b...16][ck]
[BC][ADDR][RT][b1][b2][b3][b4][b5][b6][b...16][ck]  
...  

I would like to stay away from using strings so I can more easily calculate checksums.
Also I am using Visual Studio.
Thanks for the help I can post more info if this was not clear enough.

Update So right now I think I'm reading in with something like this:

fscanf_s(in_file,"%2X", &BC);
fscanf_s(in_file,"%4X", &ADDR);
fscanf_s(in_file,"%2X", &RT);  

The I'll print out to a file like this:

fprintf_s(out_file,"%2X", BC);
fprintf_s(out_file,"%04X", ADDR);   //this pads with zeros if needed and forces 4 "digits"  
fprintf_s(out_file,"%2X", RT);  

Now I'm working on a routine for the data. Let me know if anyone has any good ideas. Thanks

+1  A: 

I would suggest a Dictionary<RT, byte[]>, and just use a single flat array. Then stride through that array calculating checksums and building the output lines, if all bytes in the line were 0xFF then you can skip appending that line to your output.

Maybe Dictionary<RT, List<byte>> if you can't predict the size of each memory space in advance, but since 4 nibbles of address only allows 64k, I'd just allocate each array to that space immediately.

Ben Voigt
Thanks, I'm not really familiar with what you are talking about, can you explain this a little more?
Nick S.
Oops, I used the .NET class names. In standard C++ that would be `map<rt_t, byte*>` (and set the pointer to an array of 64kB).
Ben Voigt
+1  A: 

I'm not sure about a 2D array -- I'd just start with a big 1D array representing (in essence) the target address space. Pre-fill it with FF. Walk through the records from the hex file, and:

  1. fill the values into your array, and
  2. keep track of the highest and lowest addresses encountered.

When you're done, start from the lowest address encountered, and encode data 10 (10h?) bytes at a time until you reach the highest address you wrote to.

Jerry Coffin
Not sure, but different record types may reference distinct address spaces, and even if not the RT definitely has to be preserved.
Ben Voigt
Thanks for the help I guess the biggest thing is how to read it in from the file.
Nick S.