5

例如,我如何转换为byte[] b = new byte[1]; b[1]=255字符串?我需要一个值为“255”的字符串变量,string text= "255";然后将其存储在文本文件中?

4

2 回答 2

14

从字节开始:

        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);
于 2012-11-21T22:20:36.937 回答
3

无需转换为字符串。你可以只使用File.WriteAllBytes

File.WriteAllBytes(@"c:\folder\file.txt", byteArray);
于 2012-11-21T22:22:47.127 回答