Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert file to binary in C#

Tags:

c#

.net

asp.net

I am trying to write a program that transfers a file through sound (kind of like a fax). I broke up my program into several steps:

  1. convert file to binary

  2. convert 1 to a certain tone and 0 to another

  3. play the tones to another computer

  4. other computer listens to tones

  5. other computer converts tones into binary

  6. other computer converts binary into file.

However, I can't seem to find a way to convert a file to binary. I found a way to convert a string to binary using

public static string StringToBinary(string data)
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in data.ToCharArray())
    {
        sb.Append(Convert.ToString(c, 2).PadLeft(8,'0'));
    }
    return sb.ToString();
}

From http://www.fluxbytes.com/csharp/convert-string-to-binary-and-binary-to-string-in-c/ . But I can't find out how to convert a file to binary (the file could be of any extension).

So, how can I convert a file to binary? Is there a better way for me to write my program?

like image 753
Daniel Avatar asked Jun 08 '14 13:06

Daniel


People also ask

What is binary file in C?

Binary file It 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 I write a BIN file?

To write to a binary file Use the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData. dat .

How do I open a text file in binary?

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.


1 Answers

Why don't you just open the file in binary mode? this function opens the file in binary mode and returns the byte array:

private byte[] GetBinaryFile(filename)
{
     byte[] bytes;
     using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
          bytes = new byte[file.Length];
          file.Read(bytes, 0, (int)file.Length);
     }
     return bytes;
}

then to convert it to bits:

byte[] bytes = GetBinaryFile("filename.bin");
BitArray bits = new BitArray(bytes);

now bits variable holds 0,1 you wanted.

or you can just do this:

private BitArray GetFileBits(filename)
{
     byte[] bytes;
     using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
          bytes = new byte[file.Length];
          file.Read(bytes, 0, (int)file.Length);
     }
     return new BitArray(bytes);
}

Or even shorter code could be:

   private BitArray GetFileBits(filename)
    {
         byte[] bytes = File.ReadAllBytes(filename);
         return new BitArray(bytes);
    }
like image 121
Ashkan Mobayen Khiabani Avatar answered Oct 21 '22 17:10

Ashkan Mobayen Khiabani