c#中的hex文件解析器 [英] hex file parser in c #

查看:213
本文介绍了c#中的hex文件解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含40000条记录的hex文件。

i想从第二行读取数据段。

i能够读取每行数据并且我已经存储了在char数组中。但那包含32个大小的数组。并且在文件中数据大小为16字节。

我想在十六进制中转换char数组。并希望与intel hex文件中包含的每一行匹配。



 openFileDialog1.Title =  选择一个Hex文件; 
openFileDialog1.DefaultExt = * .hex;
openFileDialog1.Filter = HEX Files | * .hex;
if (openFileDialog1.ShowDialog()!= System.Windows.Forms.DialogResult.Cancel&&
openFileDialog1.FileName.Length < span class =code-keyword>> 0
{
richTextBox3.Text = System.IO.Path。 GetFullPath(openFileDialog1.FileName);

}
string PathofFile = System.IO.Path.GetFullPath(openFileDialog1.FileName);
StreamReader Sr = new StreamReader(PathofFile);
string FileReader = Sr.ReadToEnd();
int CountLines = File.ReadLines(PathofFile).Count();

for int i = 1 ; i < CountLines - 2 ; i ++)
{
// string LineReader = File.ReadLines(PathofFile).Take(i).ToString();
string [] lines = File.ReadAllLines(PathofFile);
string line = lines [i];
// int ByteCount = line.Count();
// char * ptr = null;
char [] b = new char [line.Length -11];
使用(StringReader ss = new StringReader(line))
{
// ss.Read(b,9,41);
ss.Read (b, 0 9 );
ss.Read(b, 0 32 );
byte [] ByteArr = Convert.FromBase64CharArray(b, 0 ,b.Length);

string hex = BitConverter.ToString(ByteArr).Replace( < span class =code-string> - , );



来< span class =code-keyword> in string hex 不同的格式我希望它是 格式,其中包含我们的intel hex文件

解决方案

好吧,我已经制作了简单的代码来读取二进制hex文件。

 opfn.Title =  选择一个Hex文件; 
opfn.DefaultExt = * .hex;
opfn.Filter = HEX Files | * .hex;

if (opfn.ShowDialog()== DialogResult.Cancel)
return ;

// 获取所选文件名并使用二进制流打开
string strPath = System.IO.Path.GetFullPath(opfn.FileName);
BinaryReader binReader = new BinaryReader(File.Open(strPath,FileMode.Open));

// 获取文件长度和分配黄油
long lfileLength = binReader.BaseStream.Length;
字节 [] btFile = new 字节< /跨度> [lfileLength];

// 读取文件内容的每个字节
for long lIdx = 0 ; lIdx < lfileLength; lIdx ++)
{
btFile [lIdx] = binReader.ReadByte();
}



我希望这对你有很大的帮助。


:020000040000FA

:10000000B80C001061020000690200006B020000E1

:100010006D0200006F02000071020000000000008D

:10002000000000000000000000000000730200005B

:100030007502000000000000770200007902000055

:100040007B0200003D3000007B0200007B020000CC

:100050007B0200007B0200007B0200007B020000AC

:100060007B0200007B0200007B0200007B0200009C

:100070007B020000000000007B0200007B02000009

:100080007B0200007B0200007B0200007B0200007C

:100090007B0200007B0200007B0200007B0200006C

:1000A0007B0200007B0200007B0200007B0200005C

:1000B0007B0200007B0200007B0200007B0200004C

:1000C0007B0200007B0200007B0200007B0200003C

:1000D0007B0200007B0200007B0200007B0200002C

:1000E0007B020000DFF80CD00FF03CFC004800471A





i希望以同样的形式阅读它t并且想要追加所有行数据块。并且想要从第2行读取并且只有数据段需要读取在第2行的偏移之后来自B。


< blockquote> @ richard看到我有上面的解决方案,但现在我正在读取这个大小为32的字符数组(B80C001061020000690200006B020000),但现在我想读取(b8)的1字节,第二字节(oc),使它成为16字节数组,现在是32字节数组。



我的最后一个代码是

for(int i = 1;我< CountLines - 2; i ++)

{

string [] lines = File.ReadAllLines(PathofFile);

string line = lines [i];

char [] B = new char [line.Length - 11];

int charcount = B.Count();

using(StringReader Ss =新的StringReader(线))

{

Ss.Read(B,0,9);

Ss.Read(B,0, B.Length);

string Hexdata = new string(B);


i have a hex file in that have 40000 records.
i want to read data segment from 2nd line.
i am able to read data per line and i have stored in char array. but that is containing 32 size of array. and in file that data size is 16 byte.
i want to convert char array in hexadecimal.and want to match with each line contain in intel hex file.

openFileDialog1.Title = "Select a Hex file";
openFileDialog1.DefaultExt = "*.hex";
openFileDialog1.Filter = "HEX Files|*.hex";
if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel &&
openFileDialog1.FileName.Length > 0)
{
richTextBox3.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);

}
string PathofFile = System.IO.Path.GetFullPath(openFileDialog1.FileName);
StreamReader Sr = new StreamReader(PathofFile);
string FileReader = Sr.ReadToEnd();
int CountLines = File.ReadLines(PathofFile).Count();

for (int i = 1; i < CountLines - 2; i++)
{
// string LineReader = File.ReadLines(PathofFile).Take(i).ToString();
string[] lines = File.ReadAllLines(PathofFile);
string line = lines[i];
//int ByteCount = line.Count();
//char *ptr = null;
char[] b = new char[line.Length -11];
using (StringReader ss = new StringReader(line))
{
// ss.Read(b,9,41);
ss.Read(b,0,9);
ss.Read(b,0,32);
byte[] ByteArr = Convert.FromBase64CharArray(b, 0, b.Length);

string hex = BitConverter.ToString(ByteArr).Replace("-", "");



the value which is coming in string hex is in different format i want it to be in format that contain our intel hex file

解决方案

Well, I've made simple code for reading binary hex file.

 opfn.Title = "Select a Hex file";
opfn.DefaultExt = "*.hex";
opfn.Filter = "HEX Files|*.hex";

if (opfn.ShowDialog() == DialogResult.Cancel)
    return;

// get selected filename and open it with binarystream
string strPath = System.IO.Path.GetFullPath(opfn.FileName);
BinaryReader binReader = new BinaryReader(File.Open(strPath, FileMode.Open));

// get file length and alloc butter
long lfileLength = binReader.BaseStream.Length;
Byte[] btFile = new Byte[lfileLength];

// read every byte of file's content
for (long lIdx = 0; lIdx < lfileLength; lIdx++)
{
    btFile[lIdx] = binReader.ReadByte();
}


I hope this will great help you.


:020000040000FA
:10000000B80C001061020000690200006B020000E1
:100010006D0200006F02000071020000000000008D
:10002000000000000000000000000000730200005B
:100030007502000000000000770200007902000055
:100040007B0200003D3000007B0200007B020000CC
:100050007B0200007B0200007B0200007B020000AC
:100060007B0200007B0200007B0200007B0200009C
:100070007B020000000000007B0200007B02000009
:100080007B0200007B0200007B0200007B0200007C
:100090007B0200007B0200007B0200007B0200006C
:1000A0007B0200007B0200007B0200007B0200005C
:1000B0007B0200007B0200007B0200007B0200004C
:1000C0007B0200007B0200007B0200007B0200003C
:1000D0007B0200007B0200007B0200007B0200002C
:1000E0007B020000DFF80CD00FF03CFC004800471A


i want to read it in same format and want to append all lines data block.and want to read from 2nd line and only data segment need to read that comes after offset of 9.in 2nd line is going to start from B.


@ richard see i have got the above solution but now i am reading this in char array of size 32 (B80C001061020000690200006B020000) but now i want to read as 1 byte of (b8),2nd byte (oc) so that it become 16 byte array , now its 32 byte array .

my last code is
for (int i = 1; i < CountLines - 2; i++)
{
string[] lines = File.ReadAllLines(PathofFile);
string line = lines[i];
char[] B = new char[line.Length - 11];
int charcount = B.Count();
using (StringReader Ss = new StringReader(line))
{
Ss.Read(B, 0, 9);
Ss.Read(B, 0, B.Length);
string Hexdata = new string(B);


这篇关于c#中的hex文件解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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