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