如何将一个字节数组 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);
"255"
,而是返回字节值为255
的UTF8字符。 - Jesse Webb不需要转换为字符串。您可以直接使用File.WriteAllBytes。
File.WriteAllBytes(@"c:\folder\file.txt", byteArray);