Processing math: 100%
doute7910 2013-12-16 13: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 14: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部