title: PHP equivalent of Convert.ToInt32 in .NET Framework
body: <p>I'm looking for a concise way to convert a 8-char string to an 32-bit signed integer.</p>
  
  <p>See the reference for Convert.ToInt32 method on <a href="http://msdn.microsoft.com/en-us/library/1k20k614.aspx" rel="nofollow">MSDN</a>.</p>
  
  <p>This is the current .NET code in VB:</p>
  
  <pre><code>Convert.ToInt32("c0f672d4", 16)
  // returns -1057590572
  </code></pre>
  
  <p>How can I get the same return value using PHP 5.3+ for both 32-bit and 64-bit?</p>
  
  <p>I imagine it may require a combination of <a href="http://php.net/pack" rel="nofollow">pack</a>/<a href="http://php.net/unpack" rel="nofollow">unpack</a> functions and <a href="http://php.net/operators.bitwise" rel="nofollow">bitwise operators</a> but as yet have not found the right combination.</p>
  
  <p><strong>Updated: 2013-07-10</strong>
  The following <strong>only</strong> works on 32-bit systems:</p>
  
  <pre><code>$str = 'c0f672d4';
  $int = intval( substr( $str, 0, 4 ), 16 ); // read high 16 bit word
  $int &lt;&lt;= 16; // shift hi word correct position
  $int |= intval( substr( $str, 4, 4 ), 16 ); //  read low 16 bit word
  echo $int;
  // returns -1057590572
  </code></pre>
  
  <p>The problem with the above is that it <strong>does not work</strong> on 64-bit system. Instead I get the value <code>3237347344</code> using the above PHP code.</p>
  
  <p>Any ideas for getting a consistent integer with PHP which is portable for 32-bit and 64-bit?</p>