doute7910 2013-12-16 21:51
浏览 66
已采纳

PHP无法计算(二进制十六进制怪异)?

In Perl I have code that is working correctly:

print unpack('B*','10071C2');

returns 00110001001100000011000000110111001100010100001100110010

The code ported to PHP using GMP:

function gmp_convert($num, $base_a, $base_b)
{
        return gmp_strval ( gmp_init($num, $base_a), $base_b );
}
$test = "10071C2";
$testb=gmp_convert($test, 16, 2);

produces 10000000110110001110000101001101111110110001101110000111

I thought it might be byte order, however if I use b* instead in Perl it still produces something else:

PHP---10000000110110001110000101001101111110110001101110000111
PERL--10001100000011000000110011101100100011001100001001001100

I simply do not understand this, can anyone help?

  • 写回答

1条回答 默认 最新

  • douhong4452 2013-12-16 22:03
    关注

    Your Perl and PHP implementations are doing entirely separate things.

    The Perl code is converting each of the characters from the input string into the binary representation of the ASCII code for that character. For instance, the first character ("1") gets converted into "00110001" which is equal to decimal 49, the ASCII code for the character 1.

    Your PHP code successfully converts the hex number represented in string form into an equivalent binary representation in string form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真