简体   繁体   English

c#将byte转换为string并写入txt文件

[英]c# convert byte to string and write to txt file

How can i convert for example byte[] b = new byte[1]; b[1]=255 我如何转换例如byte[] b = new byte[1]; b[1]=255 byte[] b = new byte[1]; b[1]=255 to string ? byte[] b = new byte[1]; b[1]=255到字符串? I need a string variable with the value "255" string text= "255"; 我需要一个字符串变量,其值为“255” string text= "255"; and then store it in a text file? 然后将其存储在文本文件中?

Starting from bytes: 从字节开始:

        byte[] b = new byte[255];
        string s = Encoding.UTF8.GetString(b);
        File.WriteAllText("myFile.txt", s);

and if you start from string: 如果你从字符串开始:

        string x = "255";
        byte[] y = Encoding.UTF8.GetBytes(x);
        File.WriteAllBytes("myFile2.txt", y);

No need to convert to string. 无需转换为字符串。 You can just use File.WriteAllBytes 你可以使用File.WriteAllBytes

File.WriteAllBytes(@"c:\folder\file.txt", byteArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM