Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
3.22/5 (2 votes)
See more:
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.

C#
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
Posted
Updated 2-Mar-22 1:01am
v2
Comments
ZurdoDev 29-Oct-13 7:29am    
Where are you stuck?
kumar9avinash 29-Oct-13 7:36am    
ihave character arry of 32 size and i want to convert in hexadecimal bcz when i will read next line in hex file i want to append datasegment and further on.
ZurdoDev 29-Oct-13 7:36am    
Please use the improve question link to add relevant code and show us where you are stuck.
kumar9avinash 29-Oct-13 7:46am    
@ ryan now u can see the changed
Richard MacCutchan 29-Oct-13 8:28am    
You seem to be reading and re-reading the file content more times than necessary. Could you also show the exact format of one of the lines in your text file, as it is not clear what it looks like?

You can use HexIO nuget packet.
 
Share this answer
 
Comments
CHill60 2-Mar-22 8:32am    
That package wasn't available in 2013
Well, I've made simple code for reading binary hex file.
C#
 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.
 
Share this answer
 
v2
@ 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);
 
Share this answer
 
Comments
Richard MacCutchan 30-Oct-13 7:28am    
That code makes no sense to me, and is just the same as in your original question. Why not just take each two characters in turn and use the Int.Parse() method to convert to its byte value?
Also, please use the Reply buttons to respond, or the Improve question link to add additional information; don't post as Solutions.

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