0

One way or another, all digital data is stored in 0 and 1. That's the principle of binary data, I guess.

Is there a method or package that can show you the binary code of a file/single-exe-program of how it is actually being stored in the 0/1 format??

I would see it like: - import a certain, random file - convert it to it's 0/1 format - store the the 1/0-data in a txt (streamwriter/binarywriter)

if yes, is this available in any .NET language (pref: c#)?

1
  • What you see when you open a binary files in something like note pad is the set of characters that represent the binary octets. Look up an ASCII table to understand what I mean. Converting that representation to actual 0 and 1 characters effectively multiplies the file size by 8 (best case scenario) as each character you see is now an octet representing a simple bit. Hope you understand the "principle" you mentioned on your question now. Commented Jun 26, 2015 at 12:13

5 Answers 5

2

Essentially you just need to break this into two steps:

  1. Convert a file into bytes
  2. Convert a byte into a binary string

The first step is easy:

var fileBytes = File.ReadAllBytes(someFileName);

The second step is less straightforward, but still pretty easy:

var byteString = string.Concat(fileBytes.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')))

The idea here is that you select each byte individually, converting each one to a binary string (pad left so each one is 8 characters, since many bytes have leading zeroes), and concatenate all of those into a single string. (Courtesy in part of @xanatos' comment below.)

Sign up to request clarification or add additional context in comments.

9 Comments

The second line is wrong I think... string.Concat(fileBytes.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')))
@xanatos: Could be. I'm curious enough that I'm currently firing up VS to test :)
@xanatos: You are indeed correct, I'll update the answer. Thanks!
make it a stream so if the file is big it wont cause memory issue :-)
@Fredou: Ok, now it's just getting silly :) You're right, there could easily be memory problems with very large files. Most files aren't that large. Though to be fair both the input and the output would need to be streams in that case, since holding a string which is technically eight times larger than the file wouldn't be good for memory either :)
|
1

I think this is something what you are looking for:

byte [] contents = File.ReadAllBytes(filePath);
StringBuilder builder = new StringBuilder();
for(int i = 0; i<contents .Length; i++)
{
  builder.Append( Convert.ToString(contents[i], 2).PadLeft(8, '0') );
}

Now, you can for example write builder contents to a text file.

2 Comments

Please, use a StringBuilder... The world doesn't need another Schlemiel the Painter's Algorithm
@xanatos: ok changed that
1

this will stream the conversion, useful if you have huge file.

using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var buffer = new byte[1024];
            int pos = 0; 

            using (var fileIn = new FileStream(@"c:\test.txt", FileMode.Open, FileAccess.Read))
            using (var fileOut = new FileStream(@"c:\test.txt.binary", FileMode.Create, FileAccess.Write))
                while((pos = fileIn.Read(buffer,0,buffer.Length)) > 0)
                    foreach (var value in buffer.Take(pos).Select(x => Convert.ToString(x, 2).PadLeft(8, '0')))
                        fileOut.Write(value.Select(x => (byte)x).ToArray(), 0, 8);
        }
    }
}

Comments

0

You can open the file in binary mode. Didn't test it but it should work :

BitArray GetBits(string fuleSrc)
{
     byte[] bytesFile;
     using (FileStream file = new FileStream(fuleSrc, FileMode.Open, FileAccess.Read))
     {
          bytesFile = new byte[file.Length];
          file.Read(bytes, 0, (int)file.Length);
     }

     return new BitArray(bytesFile);
}

Comments

0

A solution using FileStream, StreamWriter, StringBuilder and Convert

static void Main(string[] args)
{
    StringBuilder sb = new StringBuilder();
    using (FileStream fs = new FileStream(InputFILEPATH, FileMode.Open))
    {
          while (fs.Position != fs.Length)
          {
              sb.Append(Convert.ToString(fs.ReadByte(),2));
          }
    }
    using (StreamWriter stw = new StreamWriter(File.Open(OutputFILEPATH,FileMode.OpenOrCreate)))
    {
            stw.WriteLine(sb.ToString());
    }
   Console.ReadKey();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.