3

I've been a programmer for a few years now, but I've never had to understand low-level operations involving bytes. It interests me however, and I would like to understand more about working with bytes.

In the below code I'm reading a text file that contains only the words "hi there".

        FileStream fileStream = new FileStream(@"C:\myfile.txt", FileMode.Open);

        byte[] mybyte = new byte[fileStream.Length];

        fileStream.Read(mybyte, 0, (int)fileStream.Length);

        foreach(byte b in mybyte)
            Console.Write(b);

        Console.ReadLine();

In this case, the mybyte variable contains numeric values that appear to represent the ASCII decimal counterpart. However, I thougth bytes represent bits, which in turn represnt binary values. When reading a byte I would expect to see a binary value like '0001010', not '104' which is the ascii character for 'h'.

In the case of reading an image, when reading the image into a byte array I once again see numbers in the array, and from a low-level persepctive I would expect binary values. I know that these numbers obviously don't map to Ascii, but I'm confused why when reading a string they would map to ascii numbers and when reading an image stream it does something else (I'm not actually sure what the numbers represent in the case of reading an image).

I know understanding what the numbers mean in a byte array isn't critical, but it greatly interests me.

Could someone please shed a light on bytes in the .net framework when reading from a text file and when reading binary (i.e. image). Thank You

This image is the byte array holding the text "hi there" read from myfile.txt Byte array of the myfile.txt file.  Values map to "hi there" This image is a byte array holding an image stream enter image description here

contactmatt
  • 18,116
  • 40
  • 128
  • 186
  • It's arbitrary and you can change the representation in your settings. Most of the time you want to see a number represented in decimal because it's just easier to read (I don't care to bust out a calculator when looking at a variable in the debugger), but a number's a number. You could represent it in base 104 as 1 if you really wanted to. – Ed S. Mar 14 '11 at 06:05
  • 2
    I believe you need to start with a book on computer science fundamentals. Your question shows that although you know how to program, you have very little understand why programs work, and this is a bit dangerous because you then won't understand why they fail. – Stephen Chung Mar 14 '11 at 06:12
  • You do realize that a binary numbers and decimal numbers are all the *same* numbers, right? – Gabe Mar 14 '11 at 06:19
  • @Ed S.: 104 in base 10 is the same as 10 in base 104. – Gabe Mar 14 '11 at 06:20
  • @Gabe: Haha oops. Meant to add that zero there... – Ed S. Mar 14 '11 at 07:12

4 Answers4

3

01101000 is the 8 bit representation of the value 104. Since a c# byte stores 8bits (0-255) it is shown to you as something more readable. Open up the windows calculator and change the view to "Programmer", then set it to "Bin". Might clears things up a bit.

It is not showing you a decimal number, it is showing you a c# byte, a number from 0 to 255

Dested
  • 6,294
  • 12
  • 51
  • 73
0

A byte is literally an 8-bit integer that is represented there as an integer from 0 to 255 - in other words, in decimal notation. You were expecting it to be represented in binary notation, but it actually would mean the same thing. As best I can say is that's just how Visual Studio in this case represents it but there may some more details someone can shed.

An image file is just a sequential set of bytes, again, all represented here as decimal numbers.

Hope that helps.

nycdan
  • 2,819
  • 2
  • 21
  • 33
0

A byte consists of 8 bits. Those can be written in different ways, for example as decimal value (104), as binary values (1101000) or as headecimal value (68). They all mean exactly the same, it are just different representations of the values.

This has nothing to do with ASCII-Characters. They just happen to be a byte long, too (7 bit, to be precise).

Gimno
  • 6,506
  • 3
  • 39
  • 39
0

Of course, everything at low-level will be stored as collection of binary values. What you are seeing with debugger is it's decimal representation. As binary values don't mean anything unless we interpret them, the same thing with the decimal number your seeing with the debugger in both the cases (string and image).

For example, when your read a byte from filestream and then parse it with encoding like:

FileStream fs = new FileStream(@"<Filename>", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bt = new byte[8];
            fs.Read(bt , 0, 1);
            string str = System.Text.ASCIIEncoding.ASCII.GetString(bt);

You will get a ASCII character even if your reading from a image file. If you pass the same image filestream to a Image class like

Bitmap bmp = (Bitmap)Image.FromFile(@"<Filename>");

and assign this bmp to picture box, you will see a image.

Summary: Your interpreters give the meaning to your 0's and 1's or your decimal numbers. By themselves they don't mean anything.

Typist
  • 1,464
  • 9
  • 14