Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two hex file
i want to read 50 line from First hex file and add it in another hex file From address $4000 and recalculate checksum.

please help me.
Posted
Updated 17-Feb-11 1:58am
v2
Comments
CPallini 17-Feb-11 8:32am    
Do you mean Intel (http://en.wikipedia.org/wiki/Intel_HEX) HEX files?
rakeshpatil1234 18-Feb-11 0:24am    
yes file is INTEL hex record compatible

That is not quite sensible: a Hex file does not have lines.
Ar eyou trying to raed 50 bytes from one hex file, add it to another and then work out a checksum?

If so, then look at Stream.ReadBytes:
FileStream fs = File.OpenRead(filename);
int length = Math.Min(fs.Length, 50);
byte[] data = new byte[length];
fs.Read (data, 0, length);

and File.ReadAllBytes:
byte[] otherData = File.ReadAllBytes(path);

Then just assemble what you want into a separate array:
byte[] outData = new byte[4000 + length];
Array.Copy(otherData, outdata, 4000);
Array.Copy(data,0,  outData, 4000, length)

Do your checksum - can't help you there without knowing what kind of checksum you are using...


"yes both files are INTEL hex record compatible, so when we add 50 lines at particular position then it moves other line so it changes address of each line
Please help"


In that case, you will have to read each line, process it into binary and store it.
You then insert your records with the appropriate address changes, and regenerate the file. Because each line contains an address, either you will have to replace hex values at address 4000 and on up, or insert. Either way, you need to generate the checksum: (trivial, the instructions are on the wiki page http://en.wikipedia.org/wiki/Intel_HEX[^] refered above)
You will have to use File.ReadAllLines to read each file, then convert each to binary (probably checking the checksums, it can't hurt and proves your checksum routine works)
Then merge, altering addresses as needed, and write the new file back out.

If this is a problem, then explain where and I'll try to help (without actually writing the whole app for you :laugh:)

Things to watch for: I don't know why you are doing this, but if the Hex file contains processor instructions then moving the data around may not work: For example, JUMP instructions can be coded to a specific address. If you move the data at that address, it will not move alter the JUMP instruction - it will go to the original address and execute the wrong instructions.
 
Share this answer
 
v2
Comments
CPallini 17-Feb-11 8:31am    
These http://en.wikipedia.org/wiki/Intel_HEX 'HEX' files do have lines.
OriginalGriff 17-Feb-11 8:33am    
Yes: but since he is talking about addresses within files and checksums, I suspect he means binary files... :laugh:
rakeshpatil1234 18-Feb-11 0:34am    
yes both files are INTEL hex record compatible, so when we add 50 lines at particular position then it moves other line so it changes address of each line
Please help
OriginalGriff 18-Feb-11 3:27am    
Answer updated
Intel HEX file checksum calculation is trivial,

for instance:
C#
byte Chk(byte[] data)
{
    byte sum = 0;
    foreach (byte b in data)
    {
        sum += b;
    }
    sum = (byte)~sum;
    sum++;
    return sum;
}


See intel HEX file at Wikipedia[^] for details.
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900