简体   繁体   中英

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 to string ? I need a string variable with the value "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(@"c:\folder\file.txt", byteArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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