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 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?