简体   繁体   中英

How to store byte[] in a text file in c#?

I want to store a byte[] in a text file using c#, where the file content should be between 0-255.

For instance the

byte [] =new byte[] {0,0, 255, 0, 0 ,0,189, 198, 200, 100,.....}

Content i want to see in the text file:

0,0, 255, 0, 0 ,0,189, 198, 200, 100,.....

I tried using various ways available for it:

  1. this code writes unreadable symbols in the file.

     using (var fs = new FileStream(MyFilePath, FileMode.Create, FileAccess.Write)) { fs.Write(by, 0, by.Length); }
  2. this code writes hex values like "00-00-FF-00-00-00-BD-C6-C8-64"

     File.WriteAllText(Myfilepath, BitConverter.ToString(Mybytearray));

You just need one line of code to accomplish this:

File.WriteAllText(MyFilePath, string.Join(", ", bytes));

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