Convert four bytes to Integer using C++
<p>I am trying to convert 4 bytes to an integer using C++.</p>

<p>This is my code:</p>

<pre><code>int buffToInteger(char * buffer)
{
    int a = (int)(buffer[0] &lt;&lt; 24 | buffer[1] &lt;&lt; 16 | buffer[2] &lt;&lt; 8 | buffer[3]);
    return a;
}
</code></pre>

<p>The code above works in almost all cases, for example:
When my buffer is: <code>"[\x00, \x00, \x40, \x00]"</code> the code will return <code>16384</code> as expected.</p>

<p>But when the buffer is filled with: <code>"[\x00, \x00, \x3e, \xe3]"</code>, the code won't work as expected and will return <code>"ffffffe1"</code>.</p>

<p>Does anyone know why this happens?</p>