- substr: documentation ( source)
- var_dump: documentation ( source)
- pack: documentation ( source)
- current: documentation ( source)
- unpack: documentation ( source)
<?php
const INT_SIGNED = 1;
const INT_LE = 2;
function isLittleEndian() {
return pack('S', 0x00FF) === "\xFF\x00";
}
function is32Bit() {
return PHP_INT_MAX < 0x80000000;
}
function unpack_int32($str, $flags = 0) {
$length = strlen($str);
if ($length < 4) {
return false;
} else if ($length > 4) {
$str = substr($str, 0, 4);
}
$format = ($flags & INT_LE) ? 'V' : 'N';
$result = current(unpack($format, $str));
if (($result & 0x80000000) && ($flags & INT_SIGNED) && !is32Bit()) {
$result = 0xFFFFFFFF00000000 | $result;
}
return $result;
}
var_dump(isLittleEndian(), is32Bit(), unpack_int32("\xFF\xFF\xFF\xFF", INT_SIGNED));