C# 读取16进制文件参考代码如下:
FileStream fs = new FileStream("d:\abc.aa", FileMode.Open, FileAccess.Read)BinaryReader br = new BinaryReader(fs)
StreamWriter sw = new StreamWriter("d:\\aa.txt")
int length = (int)fs.Length
while (length > 0)
{
byte tempByte = br.ReadByte()
string tempStr = Convert.ToString(tempByte, 16)
if (temStr.Length == 1) temStr = "0" + temStr
sw.Write(tempStr)
length--
}
fs.Close()
br.Close()
sw.Close()
1,文件里的数据是不同类型的,既有字符类型的,也有整型的。2,遇到这种情况,可以按照字符串类型定义数组。例如,char s[100][4]
3,编写程序时,一个一个字符读取,遇到空格就完成了一个数据读取。遇到换行,就换到下一行。
这道题目的关键是把数据类型当作字符串类型处理。
你看下面的程序中,把文本的内容都按照整型数来读取,那么当然有问题!数组s的声明是错误的!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)