c# file
C# 里,怎样写bytes[] 数组到一个文本文建里,如byte[]{0x34,0x45,0x58.......0x57},在文本文件里显示为:
0x34 0x45 0x58.......//16个数据
0x59 。。。。。。。。。//16个 数据 --------------------编程问答-------------------- 把byte[]里挨个取出来往里写就行了啊
你的问题是什么?
--------------------编程问答-------------------- 不行啊,变成了字符格式啦, --------------------编程问答-------------------- 不再是我的byte数组一样的原数据,如0x34,就变成了4了,和我想的不一样 --------------------编程问答-------------------- 你先转换成char型的 --------------------编程问答--------------------
byte[] buf=new byte[200];--------------------编程问答-------------------- 参见http://msdn.microsoft.com/zh-cn/library/2bbab5dh(VS.80).aspx --------------------编程问答--------------------
.........
StreamWriter sw = new StreamWriter("C;\\1.txt");
sw.Write(buf);
sw.Close();
sw.Dispose();
FileStream fs = new FileStream("C:\\1.txt",FileMode.OpenOrCreate,FileAccess.Write);--------------------编程问答-------------------- byte[] byData;
fs.Write(buf, 0, buf.Length);
fs.Close();
fs.Dispose();
FileStream fs= new FileStream("a.txt", FileMode.Create);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(byData, 0, byData.Length);
System.IO.File.WriteAllBytes("", byData); --------------------编程问答-------------------- 你们的方法我都试了,在文本里都变成字符格式啦,而不是字节格式,
还有什么好方法? --------------------编程问答-------------------- up --------------------编程问答--------------------
byte[] a = new byte[160];--------------------编程问答--------------------
StringBuilder s = new StringBuilder();
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 160; i++)
{
a[i] = (byte)r.Next(256);
s.Append(((i % 16==0 && i>0)?"\r\n":"") + "0x" + a[i].ToString("x") + " ");
}
StreamWriter sw = new StreamWriter("C:\\1.txt");
sw.Write(s.ToString());
sw.Close();
sw.Dispose();
肯定要变成字符,你怎么样的?
你定义都成byte[]{0x34,0x45,0x58.......0x57}了,写入文件按照asc吗对照着写入了
要写入字符串,定义string[]=new string[]{"0x34","0x45","0x58"......."0x57"};
string[] str = new string[] { "0x34", "0x45", "0x58", "0x57" };--------------------编程问答--------------------
StreamWriter sw = new StreamWriter("C:\\1.txt");
foreach (string s in str)
sw.WriteLine(s);
sw.Close();
sw.Dispose();
我的数据时从MCU读出来的byte[]数组,我想把这些数组保存到文本文件中,这样我就可以检查别人给我的数据有没有错误,
不过我还是要谢谢你, --------------------编程问答-------------------- 11楼的代码应该可以解决 --------------------编程问答-------------------- 只有11楼的朋友回答符合我的要求,不过还是要谢谢各位
补充:.NET技术 , C#