Apqu
Apqu

Reputation: 5031

Open file, read as hex and convert it to ASCII?

Is it possible to read a file hex values into c# and output the corresponding ASCII? I can view the file in a hex editor which I can then see the appropriate ASCII next to the hex but rather than manually copying out the parts I need I imagine there is a way of the machine doing it for me in a c# program?

I did find Converting HEX data in a file to ascii but that didn't really help?

Upvotes: 0

Views: 2697

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500335

It sounds like you just need:

string text = File.ReadAllText("file.txt");

There's no such thing as "hex values" in a file - they're just bytes which are shown as hex in various editors geared towards editing non-text files.

The above line of code will load a text file, decoding it as UTF-8 - which is compatible with ASCII, so if your file is truly ASCII, it should be fine. If you need to specify a different encoding, you can do it with an overload, e.g.

// Load an ISO-8859-1 file
string text = File.ReadAllText("file.txt", Encoding.GetEncoding(28591));

Upvotes: 4

Related Questions