读书人

Hex 文件处理,该如何解决

发布时间: 2013-10-17 17:26:17 作者: rapoo

Hex 文件处理
好久没再写过.net程序,很多东西都忘记了。

标准的intel格式的.hex文件一般都是如下格式的:

:10000000D855001071410000094200000B42000069

我现在需要把其中的数据部分提取出来,显示在texbox里面,后面还需要发送给下位机,
这个是从hex文件里读出来的字符串。
D855001071410000094200000B420000

请教一下各位大哥:
1.怎么把数据部分的字符串按每两位转成一个16进制byte显示?
D8 55 00 10 71 41 00 00 09 42 00 00 0B 42 00 00

我用for循环,每次读两个字符,然后不知道怎么才能转到16进制的byte数组里去
[code]
Dim sr As StreamReader = New StreamReader(File_Path, System.Text.Encoding.GetEncoding("GB2312"))
Dim line As String
Do
line = sr.ReadLine()
If (InStr(line, ":10")) = 1 Then
For i = 0 To 15
data_16byte(i) = encText.GetBytes(Mid(line, 10 + i, 2), )//怎么弄?
Next
appendBytes(Me.RichTextBox1, data_16byte,Me.charsInline, True)
[/code]

2.上面代码每次只读一行,然后还要处理load textbox显示,原hex文件最大有180k,怎么才能加快load的速度?

谢谢大家
VB.net? .hex?file 字符串处理;
[解决办法]



public static string ConvertStringToHex(string sts)
{
string hex = string.Empty;
try
{
byte[] b = Encoding.GetEncoding("GB2312").GetBytes(sts);
for (int i = 0; i < b.Length; i++)
{
string t = b[i].ToString("X");
if (t.Length == 1)
{
t = "0" + t;
}
hex += t + " ";
}
}
catch
{
hex = "";
}
return hex;
}

public static byte[] ConvertHexStrtoByte(string inSting)
{
inSting = DelGeLi(inSting);//去掉隔离符

byte[] strBt = new byte[inSting.Length / 2];
for (int i = 0, j = 0; i < inSting.Length; i = i + 2, j++)
{
try
{
string s = inSting.Substring(i, 2);
try
{
if (s == "\r\n")
{


strBt[j] = (byte)Convert.ToInt16("0D", 16);
}
else
{
strBt[j] = (byte)Convert.ToInt16(s, 16);
}
}
catch
{

}
}
catch
{
throw new SystemException("HexString Error!");
}
}
return strBt;
}

private static string DelGeLi(string inString)
{
string outString = "";
try
{
string[] del = { " ", "0x", "0X" };//,"\r\n","\r","\n" };
if (inString.Contains(" ")
[解决办法]
inString.Contains("0x")
[解决办法]
inString.Contains("0X"))//存在隔离符
{
string[] strS = inString.Split(del, System.StringSplitOptions.RemoveEmptyEntries);//以隔离符进行转换数组,去掉隔离符,去掉空格。

for (int i = 0; i < strS.Length; i++)
{
outString += strS[i].ToString();
}
//return outString;
}
else//不存在隔离符
{
return inString;
}
}
catch
{
outString = "";
}
return outString;
}


[解决办法]
Hex 文件处理,该如何解决

这样不灵活。。。
最好可以解析出来。。。
而且Intel Hex文件有结尾的。。。


[/code]



Try
Dim szLine As [String] = ""
Dim szHex As [String] = ""
Dim szHexActualContent As [String] = ""
If szHexPath = "" Then
MessageBox.Show("Please Select a HEX File!", ErrorOccurs)
Return
End If

Dim HexReader As New StreamReader(szHexPath)
Dim i As Integer = 0
While True
szLine = HexReader.ReadLine()
'读取一行数据
If szLine Is Nothing Then
'读完所有行
Exit While
End If
If szLine.Substring(0, 1) = ":" Then
'判断第1字符是否是:

If szLine.Substring(1, 8) = "00000001" Then
'数据结束
Exit While
End If
'读取有效字符
i += 1
szHex += szLine.Substring(9, szLine.Length - 11) ' + Chr(13)
szHexActualContent += szLine.Substring(9, szLine.Length - 11)
End If
End While

txtHexContent.Text = szHex

HexReader.Close()
'关闭目标文件

Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try

[/code]
[解决办法]
在数据处理这块,还是都了解String类的函数用法,如:
dim i as integer = 100
dim s as string = i.string("X2") ' s = 64 强制显示2位16进制,没有则补0

读书人网 >VB Dotnet

热点推荐