DELTA12
DELTA12

Reputation: 209

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

Im wondering how to save data in other types of file than text file, for example save it to raw file without any encoding, I mean just save a byte value, so you will get something like this when trying to edit it in text editor:https://i.sstatic.net/RyhWS.png

Upvotes: 0

Views: 209

Answers (1)

w5l
w5l

Reputation: 5766

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)

Upvotes: 1

Related Questions