Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
the below code is working fine , the problem is that its taking more than 15 min to read data, since the hex file has more 20 thousand line. is there any fast way to do.

2) i have tried using string builder but i am getting lots of error. plz give code example how to implement string builder rather than string.

C#



C#
public void LoadImageSize()
        {
            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[] lines = File.ReadAllLines(PathofFile);
                string line = lines[i];
                 using (StringReader CharReader = new StringReader(line))
                {
                    int offset = 9;
                    int length = line.Length - 11;
                    string Hexdata = line.Substring(offset, length);
                    Int64 lengthHexData = Hexdata.Length;
                    byte[] Hexintobyte = StringToByteArray(Hexdata);
                    Int64 ImageSize = Hexintobyte.Length;
                    TotalImageSize += ImageSize;

               }


             }
            Int64 SizeofDataBlock = TotalImageSize;

            Sr.Close();
            }

        //Methode for converting into 16 Byte Start
        public static byte[] StringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int j = 0; j < NumberChars; j += 2)
                bytes[j / 2] = Convert.ToByte(hex.Substring(j, 2), 16);
            return bytes;
        }
Posted

Quote:
for (int i = 1; i < CountLines-2; i++)
{
string[] lines = File.ReadAllLines(PathofFile);
You are reading the whole file about CountLines times.
You should put the last line before the for statement.


[update]
Quote:
int offset = 9;
int length = line.Length - 11;
string Hexdata = line.Substring(offset, length);
Int64 lengthHexData = Hexdata.Length;
byte[] Hexintobyte = StringToByteArray(Hexdata);
Int64 ImageSize = Hexintobyte.Length;
TotalImageSize += ImageSize;

I looks to me you are doing many unnecessary operations. In order to get ImageSize value, you could simply write
C#
ImageSize = (line.Length - 20) / 2;

Am I wrong?
[/update]
 
Share this answer
 
v2
Comments
kumar9avinash 6-Nov-13 4:58am    
@cpallini it dosent affect the the time..
Rob Philpott 6-Nov-13 5:32am    
CPallini is quite correct - you are reading the whole thing in each iteration of the loop. That *will* affect performance.
You can use the following

C#
StringBuilder  str = new StringBuilder();
str.Append();


Do let me know if it doesn't works.

And yes you need to implement following Namespace

C#
using System.Text;
 
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