Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Reading bytes, what are they and what's going on. I expect binary values, not decimal numbers
    primarykey
    data
    text
    <p>I've been a programmer for a few years now, but I've never <em>had</em> to understand low-level operations involving bytes. It interests me however, and I would like to understand more about working with bytes.</p> <p>In the below code I'm reading a text file that contains only the words "hi there".</p> <pre><code> 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(); </code></pre> <p>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'.</p> <p>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).</p> <p>I know understanding what the numbers mean in a byte array isn't critical, but it greatly interests me.</p> <p>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</p> <p><em>This image is the byte array holding the text "hi there" read from myfile.txt</em> <img src="https://i.stack.imgur.com/0LCOV.jpg" alt="Byte array of the myfile.txt file. Values map to &quot;hi there&quot;"> <em>This image is a byte array holding an image stream</em> <img src="https://i.stack.imgur.com/zvph4.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload