duangong0690 2013-05-16 22:38
浏览 42

PHP中二进制到十六进制转换的限制

I need to convert a binary string into a hex string. This code works fine:

<?
$binary = "1111111111111111111111101000011111111111111111110000001000100100";
$hex = dechex(bindec($binary));
echo $hex;
?>

However, if I add one extra bit (or more) to the binary string, the result just comes back at 0.

<?
$binary = "11111111111111111111111101000011111111111111111110000001000100100";
$hex = dechex(bindec($binary));
echo $hex;
?>

I guess I can break up the binary string into chunks and then combine the hex value at the end, but is there any easier way?

  • 写回答

1条回答 默认 最新

  • douqipi9704 2013-05-16 22:44
    关注

    Your function imlicitly converts the number to int, hitting the 32bit limit. BCMath calculates with one bit at a time and takes numbers contained in strings, so you can multiply thousands of digits.

    Unfortunately there is no native BCMath function for changing a numbers base, but the needed function to do so is done in a minute. Check out the link.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计