Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file binary in C#?

Tags:

c#

binary

I want to make a method that takes any file and reads it as an array of 0s and 1s, i.e. its binary code. I want to save that binary code as a text file. Can you help me? Thanks.

like image 462
Boris Avatar asked Mar 11 '10 15:03

Boris


People also ask

How do I read a binary file in C?

Use the fread Function to Read Binary File in C FILE* streams are retrieved by the fopen function, which takes the file path as the string constant and the mode to open them. The mode of the file specifies whether to open a file for reading, writing or appending.

How do I view a binary file?

To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.

What is a binary file in C?

Binary fileIt contains 1's and 0's, which are easily understood by computers. The error in a binary file corrupts the file and is not easy to detect. In binary file, the integer value 1245 will occupy 2 bytes in memory and in file. A binary file always needs a matching software to read or write it.

How do you read and write binary files?

We can make use of BinaryReader and BinaryWriter class for reading and writing binary files respectively. While creating object of BinaryReader and BinaryWriter we have to pass stream object as constructor argument. First we create the object of FileStream class which references to the specified file.


2 Answers

Quick and dirty version:

byte[] fileBytes = File.ReadAllBytes(inputFilename); StringBuilder sb = new StringBuilder();  foreach(byte b in fileBytes) {     sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));   }  File.WriteAllText(outputFilename, sb.ToString()); 
like image 108
Chris Doggett Avatar answered Sep 29 '22 08:09

Chris Doggett


Well, reading it isn't hard, just use FileStream to read a byte[]. Converting it to text isn't really generally possible or meaningful unless you convert the 1's and 0's to hex. That's easy to do with the BitConverter.ToString(byte[]) overload. You'd generally want to dump 16 or 32 bytes in each line. You could use Encoding.ASCII.GetString() to try to convert the bytes to characters. A sample program that does this:

using System; using System.IO; using System.Text;  class Program {     static void Main(string[] args) {         // Read the file into <bits>         var fs = new FileStream(@"c:\temp\test.bin", FileMode.Open);         var len = (int)fs.Length;         var bits = new byte[len];         fs.Read(bits, 0, len);         // Dump 16 bytes per line         for (int ix = 0; ix < len; ix += 16) {             var cnt = Math.Min(16, len - ix);             var line = new byte[cnt];             Array.Copy(bits, ix, line, 0, cnt);             // Write address + hex + ascii             Console.Write("{0:X6}  ", ix);             Console.Write(BitConverter.ToString(line));             Console.Write("  ");             // Convert non-ascii characters to .             for (int jx = 0; jx < cnt; ++jx)                 if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';             Console.WriteLine(Encoding.ASCII.GetString(line));         }         Console.ReadLine();     } } 
like image 22
Hans Passant Avatar answered Sep 29 '22 07:09

Hans Passant