简体   繁体   English

如何将字节[]存储在c#中的文本文件中?

[英]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.我想使用 c# 在文本文件中存储一个 byte[],其中文件内容应介于 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"此代码写入十六进制值,如“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));

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

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