0

I want to read binary files as plain text.

Is it possible to do so ?

Want to read whole file as plain text I have managed to view the data with hex viewer freefileviewer, but thing is I want to do it via coding in c#. The binary file I am trying to read is some devices input which operates directly from the binary file i need to modify certain contents of the binary file and resend it for processing.

zeeshan
  • 39
  • 9
  • Why can't you just read the binary file as a binary file? (I.e. as a `byte[]`?) From what you describe it doesn't really contain text. – millimoose Dec 19 '13 at 09:05
  • see here: http://stackoverflow.com/questions/16651746/converting-binary-data-to-string-in-persian?rq=1 – o_weisman Dec 19 '13 at 09:05
  • possible duplicate of [Binary To Corresponding ASCII String Conversion](http://stackoverflow.com/questions/6006425/binary-to-corresponding-ascii-string-conversion) – Igl3 Dec 19 '13 at 09:06
  • To the auto-downvoter of the "hexviever" answers: read carefully: `have managed to view the data with hex viewer freefileviewer, but thing is I want to do it via coding in c#`. That's why these answers are posted. If you downvote, at least add some comment why so. – quetzalcoatl Dec 19 '13 at 09:22
  • @millimoose I am reading binary file as binary but I have to modify its contents as well for processing. I am unable to parse the raw data into readable format so that I can modify the certain parameter values if there is a way around I am all ears. – zeeshan Dec 19 '13 at 10:35

2 Answers2

2

Try this to convert binary to plain text:

var binData = File.ReadAllBytes("C:\\path\\to\\file.bin");
var sb = new StringBuilder();
foreach(var b in binData)
    sb.Append(b.ToString("X2"));
var strData = sb.ToString();

You can edit it after and convert back to bytes. You can output text data to a user and convert back like this:

var bytes = new List<byte>();
for (int i = 0; i < strData.Length; i+=2)
{
    bytes.Add(Byte.Parse(strData[i].ToString() + strData[i+1].ToString(),
       NumberStyles.HexNumber));
}
File.WriteAllBytes("C:\\path\\to\\file.bin", bytes.ToArray());
Tony
  • 7,345
  • 3
  • 26
  • 34
  • I'm not sure how this would at all help process raw device input data. – millimoose Dec 19 '13 at 09:07
  • The question is about binary file! – Tony Dec 19 '13 at 09:08
  • +1: I don't know why have this been downvoted. Downvoters, please comment. The code in this answer seems to encode/decode the data "as the HexViever" just as the author asked for (unless there are bugs I didnt notice). Of course keeping data as hexstring is a nonsense, but **it this does not** make this answer invalid. – quetzalcoatl Dec 19 '13 at 09:20
  • @quetzalcoatl Thanks for support! Double downvotes without any explanation! – Tony Dec 19 '13 at 09:21
  • Most probably, because the first line says "as plain text" and hex-view is certainly not it. However, authors says "have managed to view the data with hex viewer freefileviewer, but thing is I want to do it via coding in c#", which the downvoter probably missed/misread. Just guessing though. Anyways, this question should be closed ;) – quetzalcoatl Dec 19 '13 at 09:24
-2

You should read with FileStream and then convert with ToBase64String. See this example

string myString;
using (FileStream fs = new FileStream("\\YourPath", FileMode.Open))
using (BinaryReader br = new BinaryReader(fs))
{
    byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
    myString = Convert.ToBase64String(bin);
}
Community
  • 1
  • 1
Behrad Farsi
  • 1,110
  • 13
  • 25