Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this `int` have the wrong value?
    primarykey
    data
    text
    <p>Not sure how else to put that, but I'll start off with a code snippet and output:</p> <pre><code>uint32_t expires; cout &lt;&lt; "Expiration bytes: " &lt;&lt; setfill('0') &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; (unsigned short)rec[keyLen+4] &lt;&lt; setw(2) &lt;&lt; (unsigned short)rec[keyLen+5] &lt;&lt; setw(2) &lt;&lt; (unsigned short)rec[keyLen+6] &lt;&lt; setw(2) &lt;&lt; (unsigned short)rec[keyLen+7] &lt;&lt; endl; expires = ntohl(*(uint32_t*)&amp;rec[keyLen+4]); cout &lt;&lt; "Expiration: " &lt;&lt; (long)expires &lt;&lt; endl; cout &lt;&lt; "Hex: " &lt;&lt; hex &lt;&lt; expires &lt;&lt; endl; </code></pre> <p>Outputs:</p> <pre><code>Expiration bytes: 00000258 Expiration: 258 Hex: 258 </code></pre> <p>I can confirm from other parts of the program that examining and outputting the hex representation of bytes works as expected, and that those are indeed the bytes in the byte stream (sent from another application).</p> <p>Now, I would be able to understand a bit better if <code>expiration</code> just held some nonsense, because that would mean there's some egregious error (probably involving pointers). But this... this is clearly just spitting out the hex value as if it were a decimal, and that's plain <em>wrong</em>.</p> <p>To make matters more confusing, this works at another point in the program:</p> <pre><code>fullSize = ntohs(*(uint16_t*)&amp;buff[0]); </code></pre> <p>With a byte value of 0x0114, <code>fullSize</code> will contain the value 276.</p> <p>So the question is, what the heck is going on here? How is it possible for an int to be <em>wrong</em>?</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