3

I am loading a file into a byte[]. By my understanding the byte[] should contain a specific elements of bytes (8-bit). When i print each byte, all of them are not 8-bit (i.e. they dont have the length of 8). My Code:

FileStream stream = File.OpenRead(@"C:\Image\Img.jpg");
byte[] fileByte = new byte[stream.Length];
stream.Read(fileByte, 0, fileByte.Length);

for (int i = 0; i <= fileByte.Length - 1; i++)
{
  Console.WriteLine(Convert.ToString(fileByte[i], 2));
}

Output:

10001110
11101011
10001100
1000111
10011010
10010011
1001010
11000000
1001001
100100

I think my understanding is wrong here, Can you please let me know (or provide me some tutorial links) where I am missing this.

1
  • 4
    byte[] data = File.ReadAllBytes(@"FilePath.jpg"); Commented Oct 29, 2009 at 15:53

5 Answers 5

18

Leading 0's don't get printed.

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

6 Comments

Amazing, all the "love" you're getting from this simple response... (no jealousy on my part, you beat us all time wise, plus I'm 200 for the day), but isn't it funny when we get so many reps for these simple questions?
@mvj - No kidding! But I AM jealous. But big enough to deal with it. (grin).
@mjv my highest rep answers have usually been the easiest to answer. Questions involving specialized knowledge, lots of references, etc rarely get more than a few up votes just because so few actually know if the answer is correct or not. Probably the same for mmyers and I'm sure he's happy to take the rep even if it's for an easy one.
Yeah, especially considering that my entire knowledge of C# consists of things I've read on SO. :)
Yeah... And he was the quickest to answer. Good for him!
|
13

When converting a numeric to a string, you lose any leading zeros. (Note that all of your entries start with "1".) You can use PadLeft to put them back in.

   FileStream stream = File.OpenRead(@"C:\Image\Img.jpg"); 
   byte[] fileByte = new byte[stream.Length]; 
   stream.Read(fileByte, 0, fileByte.Length); 

   for (int i = 0; i <= fileByte.Length - 1; i++) 
   { 
      Console.WriteLine(Convert.ToString(fileByte[i], 2).PadLeft(8,'0')); 

   }

4 Comments

Thanks, this is the part that I was missing. (I have only a vague knowledge of C#.)
Instead of PadLeft(8,Convert.ToChar("0")), just do PadLeft(8,'0'). No sense converting the string to a char every single time.
Man,you all beat me to the answer because I toook the time to write and test the "fix" code." Still, + to everyone who answered correctly.
@Kevin, Thank you. I don't work with the char data type a lot, so I didn't realize the single quotes made teh difference. Changing my answer to match.!
4

They all have 8 bits, but the non significant zeroes (the zeroes on the left) are not printed.

Comments

3

It is simply that the leading zeros are not included...

Comments

2

Are the bytes without leading zeros? You kinda chose a bad example because we do not know the decimal values you are displaying (ok maybe someone who knows the header structure for a .jpg file knows). I'm willing to bet leading zeros are not displayed in the binary equivalents.

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.