dounouxi1020 2018-01-08 08:04
浏览 127
已采纳

二进制数据在PHP中不由1和0表示

Let's assume the following hex string: c3b5394f6ea32eb9339ab3f9dd971f85. Using the function hex2bin I receive an error that the hex string must be an even length, so first of all, why is that?

Second, even if I remove the last character, instead of receiving a binary represented by 0's and 1's, I these characters: ��(sl˥�m��. How can I convert it to 0's and 1's? And why can't I convert an odd length hex string to binary?

  • 写回答

1条回答 默认 最新

  • douxian4323 2018-01-08 08:43
    关注

    There are a few problems with what your trying to do.

    Firstly, according to the manual for hex2bin (http://php.net/manual/en/function.hex2bin.php) ...

    Caution This function does NOT convert a hexadecimal number to a binary number. This can be done using the base_convert() function.

    So your trying to use it for something it doesn't do - admittedly, I think this may be seen as a bad naming convention in PHP. Nothing new there.

    Secondly - this is expecting to convert the string to a number and a hex string of 'c3b5394f6ea32eb9339ab3f9dd971f85' has way to many digits to be represented as a numeric value (consider that each char gives 4 bits of value).

    Following the hint in the manual, If you use some smaller examples - this is what you can get...

    echo  hex2bin('71f8').PHP_EOL;
    echo base_convert ('71f8' , 16 , 2 ).PHP_EOL;
    echo decbin(12);
    

    gives...

    q�
    111000111111000
    1100
    

    As for having to have an even number of chars. As most systems use 2 hex chars to represent a single ascii char, this means it can't use an odd char to do the conversion, so it gives a warning and ignores the last char.

    Update:

    To convert large numbers, you can write a simple function to convert each char at a time and build up the result as a string...

    function strToBin ( $number )    {
        $result = '';
        for ( $i = 0; $i < strlen($number); $i++ ){
            $conv = base_convert($number[$i], 16, 2);
            $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
        }
        return $result;
    }
    echo strToBin('d288a775eba4132a982701fa625c9b820').PHP_EOL;
    echo '110100101000100010100111011101011110101110100100000100110010101010011000001001110000000111111010011000100101110010011011100000100000';
    

    (The second output is the result from using the conversion page you indicate to check that the result is working.)

    Outputs...

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?