Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To print all bytes in an <code>int</code>? Remember that an <code>int</code> is 32-bit, which is four bytes. Reading it into a <code>char</code> buffer makes it easier to access those four bytes in the <code>int</code>.</p> <p><strong>Edit:</strong> Little explanation of the <code>int</code> type...</p> <p>Lets say you have an <code>int</code>:</p> <pre><code>int someIntValue = 0x12345678; </code></pre> <p>This is stored in 32 bits in the memory. As a single byte (<code>char</code>) is 8 bits, there are four bytes to an <code>int</code>. Each byte in the <code>int</code> can be accessed by using a <code>char</code> array or pointer:</p> <pre><code>char *someCharPointer = (char *) &amp;someIntValue; </code></pre> <p>Now you can access those four separate bytes, and see their values:</p> <pre><code>for (int i = 0; i &lt; sizeof(int); i++) printf("someCharPointer[%d] = 0x%02x\n", i, someCharPointer[i]); </code></pre> <p>The above will print (on a little-endian machine such as x86):</p> <pre> someCharPointer[0] = 0x78 someCharPointer[1] = 0x56 someCharPointer[2] = 0x34 someCharPointer[3] = 0x12 </pre> <p>If you now change <code>someIntValue</code> to the number <code>1</code></p> <pre><code>someIntValue = 1; </code></pre> <p>and print it out again, you will see <em>this</em> result:</p> <pre> someCharPointer[0] = 0x00 someCharPointer[1] = 0x00 someCharPointer[2] = 0x00 someCharPointer[3] = 0x01 </pre> <h2>Memory layout of an <code>int</code></h2> <p>If you have a variable of type <code>int</code> stored in memory with the value <code>0x12345678</code>, it's stored like this:</p> <pre> 8 bits ,----^---. | | +--------+--------+--------+--------+ |00111000|01010110|00110100|00010010| +--------+--------+--------+--------+ | | `-----------------v-----------------' | 32 bits </pre> <p>This <code>int</code> is the same as the four bytes (or <code>char</code>) <code>0x78</code>, <code>0x56</code>, <code>0x34</code> and <code>0x12</code>.</p> <p>However if we change the <code>int</code> to the number <code>1</code> then it's stored like this:</p> <pre> 8 bits ,----^---. | | +--------+--------+--------+--------+ |00000000|00000000|00000000|00000001| +--------+--------+--------+--------+ | | `-----------------v-----------------' | 32 bits </pre> <p>This <code>int</code> is the same as the four bytes (or <code>char</code>) <code>0x00</code>, <code>0x00</code>, <code>0x00</code> and <code>0x01</code>.</p> <p>So now you hopefully can see how reading as an <code>int</code> and printing as <code>char</code> will display a different result from reading and <code>int</code> and printing it as an <code>int</code>.</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.
    1. VO
      singulars
      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