C# - Reading bytes, what are they and what's going on. I expect binary values, not decimal numbers
I've been a pro开发者_开发技巧grammer 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
This image is a byte array holding an image stream01101000 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
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.
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).
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.
精彩评论