问题描述
嗨
以下代码工作正常,问题是它花了超过15分钟读取数据,因为hex文件有更多2万行。有什么快速的方法吗。
2)我尝试过使用字符串生成器但是我收到了很多错误。 plz给出代码示例如何实现字符串构建器而不是字符串。
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.
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;
}
推荐答案
for(int i = 1; i< CountLines-2; i ++)
{
string [] lines = File.ReadAllLines(PathofFile);
for (int i = 1; i < CountLines-2; i++)
{
string[] lines = File.ReadAllLines(PathofFile);
您正在阅读关于 CountLines
次的整个文件。
您应该将之前的最后一行放在<$ i $ c> for 声明。
[更新]
You are reading the whole file about CountLines
times.
You should put the last line before the for
statement.
[update]
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;
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;
我看着你做了很多不必要的操作。为了获得 ImageSize
值,你可以简单地写
I looks to me you are doing many unnecessary operations. In order to get ImageSize
value, you could simply write
ImageSize = (line.Length - 20) / 2;
我错了吗? br />
[/ update]
Am I wrong?
[/update]
StringBuilder str = new StringBuilder();
str.Append();
如果它不起作用,请告诉我。
是的你需要实现以下命名空间
Do let me know if it doesn't works.
And yes you need to implement following Namespace
using System.Text;
这篇关于字符串花了很多时间读取hex文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!