【发布时间】:2013-07-07 07:35:10
【问题描述】:
我正在寻找一种将 8 字符字符串转换为 32 位有符号整数的简洁方法。
请参阅 MSDN 上的 Convert.ToInt32 方法参考。
这是当前在 VB 中的 .NET 代码:
Convert.ToInt32("c0f672d4", 16)
// returns -1057590572
如何使用 PHP 5.3+ 为 32 位和 64 位获得相同的返回值?
我想它可能需要pack/unpack 函数和bitwise operators 的组合,但还没有找到合适的组合。
更新:2013-07-10 以下仅适用于 32 位系统:
$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
上面的问题是它在 64 位系统上不起作用。相反,我使用上面的 PHP 代码得到了 3237347344 的值。
有什么想法可以使用可移植到 32 位和 64 位的 PHP 获得一致的整数?
【问题讨论】:
-
上面的那个sn-p,虽然不会产生你想要的输出,但可以用hexdec替换
标签: c# php .net vb.net 32bit-64bit