在C#中从十六进制文件读取和写入 [英] Read and Write from Hex File in C#

查看:749
本文介绍了在C#中从十六进制文件读取和写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个十六进制文件
我想从第一个十六进制文件中读取50行,并将其添加到来自地址$ 4000的另一个十六进制文件中,并重新计算校验和.

请帮助我.

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.

推荐答案

4000并重新计算校验和.

请帮助我.
4000 and recalculate checksum.

please help me.


这不太明智:十六进制文件没有行.
Ar eyou试图从一个十六进制文件中提取50个字节,然后将其添加到另一个文件中,然后计算出校验和?

如果是这样,请查看Stream.ReadBytes:
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);


和File.ReadAllBytes:


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)


做您的校验和-在不知道您使用哪种校验和的情况下无法为您提供帮助...


"是的,这两个文件都是INTEL十六进制记录兼容的,因此当我们在特定位置添加50行时,它会移动另一行,从而更改每行的地址
请帮助"


在这种情况下,您将必须读取每一行,将其处理为二进制并存储.
然后,插入记录并进行适当的地址更改,然后重新生成文件.因为每行都包含一个地址,所以您将不得不替换地址4000或更高位置的十六进制值,或者插入.无论哪种方式,您都需要生成校验和:(平凡,说明在Wiki页面 http://en.wikipedia.org/上面提到的wiki/Intel_HEX [ ^ ])
您将必须使用File.ReadAllLines来读取每个文件,然后将每个文件转换为二进制文件(可能检查校验和,不会造成伤害并证明您的校验和例程有效)
然后合并,根据需要更改地址,然后将新文件写回.

如果出现问题,请说明问题所在,我将尽力提供帮助(实际上并没有为您编写整个应用程序:笑:)

需要注意的事情:我不知道为什么要这样做,但是如果Hex文件包含处理器指令,则移动数据可能不起作用:例如,可以将JUMP指令编码为特定地址.如果将数据移至该地址,它将不会更改JUMP指令-它将移至原始地址并执行错误的指令.


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.


Intel HEX文件校验和的计算很简单,

例如:
Intel HEX file checksum calculation is trivial,

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



请参阅Wikipedia上的英特尔HEX文件 [ ^ ].
:)



See intel HEX file at Wikipedia[^] for details.
:)


这篇关于在C#中从十六进制文件读取和写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆