c#将byte转换为string并写入txt文件
我如何转换例如byte[] b = new byte[1]; b[1]=255
byte[] b = new byte[1]; b[1]=255
到字符串? 我需要一个字符串变量,其值为“255” string text= "255";
然后将其存储在文本文件中?
从字节开始:
byte[] b = new byte[255]; string s = Encoding.UTF8.GetString(b); File.WriteAllText("myFile.txt", s);
如果你从字符串开始:
string x = "255"; byte[] y = Encoding.UTF8.GetBytes(x); File.WriteAllBytes("myFile2.txt", y);
无需转换为字符串。 你可以使用File.WriteAllBytes
File.WriteAllBytes(@"c:\folder\file.txt", byteArray);