十六进制编辑器从二进制文件读取为ascii [英] Hex editor reading as ascii from binary file
问题描述
大家好我有问题我正在尝试做一个hexeditor我可以在十六进制模式下阅读bin文件但是我不能把它读成ascii。
我正在使用这个函数
hello everybody I have question I am trying to do a hexeditor and I can read bin file in hex mode but I cannot read it as ascii.
I am using this function
private static string hx2Asc(string hxStr)
{
byte[] tmp;
int j = 0;
tmp = new byte[(hxStr.Length)/2];
for(int i = 0; i< hxStr.Length - 2; i += 2)
{
tmp[j] = (byte)Convert.ToChar(Int32.Parse(hxStr.Substring(i,2),NumberStyles.HexNumber));
j++;
}
return Encoding.GetEncoding(1252).GetString(tmp);
}
这个叫这个函数
and this to call this function
string str = rtb.Text.Replace(" ", "");
str = str.Replace("\n", "");
str = hx2Asc(str);
textBox4.Text = str;
我能看到吗?24445098 LS这个在文本框中其他字符没有来。
现在谢谢
I can read just 24445098 LS this in textbox the rest of the characters is not coming.
Thank you for now
推荐答案
如果你想要一个十六进制编辑器,你要做的第一件事就是停止使用字符串:usebyte
值始终为 - 字符串包含长度可变的unicode字符,不必为8位。
将文件作为字节读取,将其存储为字节和将其转换为ASCII值仅在您需要时显示 - 这非常简单:
The first thing you want to do if you want a hex editor is to stop using strings: usebyte
values at all times - strings contain unicode characters which are of variable length, and do not have to be 8 bits.
Read your file as bytes, store it as bytes and convert it to ASCII values for display only when you need to - which is pretty simple:
byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);
很可能值得你在你调用它之前将你感兴趣的字节数组的区域复制到一个新的短数组中(因为它将整个数组转换为字符串)。请注意,字符串中的值是字节的unicode解释为ASCII值,而不是ASCII值本身,因此从字符串生成新的二进制文件不一定会为您提供与开始时相同的文件 - 因此将它保存为字节数组上面的注释。
It may well be worth your copying the area of the byte array you are interested in into a new short array before you call this (as it converts the whole array to a string). Do note that the values in the string are the unicode interpretation of the byte as an ASCII value, not ASCII values themselves, so generating a new binary file from the string will not necessarily give you the same file as you started with - hence the "keep it as a byte array" comment above.
这篇关于十六进制编辑器从二进制文件读取为ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!