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();
.........
StreamWriter sw = new StreamWriter("C;\\1.txt");
sw.Write(buf);
sw.Close();
sw.Dispose();
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);
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[]数组,我想把这些数组保存到文本文件中,这样我就可以检查别人给我的数据有没有错误,
不过我还是要谢谢你,