我是从 Java 转向 C# 的新手。我正在尝试使用十六进制 IO 读取文件。当我读取第一个字节时,我看不到我在十六进制编辑器中看到的内容。
我在用着
StreamReader reader = new StreamReader(fileDirectory);
int hexIn;
String hex;
for (int i = 0; (hexIn = reader.Read()) != -1; i++){
hex = Convert.ToString(hexIn, 16);
}
在Java中我用过
FileInputStream fis = new FileInputStream(file);
long length = file.length();
int hexIn;
String hex = "";
for(int i = 0; (hexIn = fis.read()) != -1; i++){
String s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
我希望这是有道理的。非常感激任何的帮助 :)
Thanks.
不要使用StreamReader
——这仅适用于某种编码(默认 UTF8)的字符。用一个FileStream http://msdn.microsoft.com/en-us/library/y0bs3w9t.aspx类代替:
FileStream fs = new FileStream(fileDirectory, FileMode.Open);
int hexIn;
String hex;
for (int i = 0; (hexIn = fs.ReadByte()) != -1; i++){
hex = string.Format("{0:X2}", hexIn);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)