简体   繁体   中英

How to save data to other than text file in C#

我想知道如何将数据保存到文本文件以外的其他类型的文件中,例如,将其保存到未经任何编码的原始文件中,我的意思是只保存一个字节值,所以当您尝试在文本编辑器中对其进行编辑时,您将得到类似的内容: http://i.imgur.com/jVh2Ksk.png

You'll want to convert your string to a byte array. See the following SO answer: String to raw byte array . You can then write the byte array to a file.

In the System.Text.Encoding namespace are some encodings with methods you can use to convert a string to a byte array easily. Depending on the contents of your string you could use eg:

var bytes = Encoding.ASCII.GetBytes(input);
var bytes = Encoding.UTF8.GetBytes(input);
var bytes = Encoding.UTF32.GetBytes(input);

Writing to a file can be easily archieved by:

File.WriteAllBytes(string path, byte[] 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