Convert binary to hexadecimal using PHP

前端 未结 4 1684
鱼传尺愫
鱼传尺愫 2021-01-28 15:06

How do I convert a binary number (i.e. 1111111) to hexadecimal (i.e. 7f) using PHP? I recognize I could do dechex(bindec(\'1111111\'));, h

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 15:21

    Your solution is fine. You can also use base_convert.

    $binary = '1111111';
    echo base_convert($binary, 2, 16); // 7f
    

    But keep in mind that php is not built for calculations. It is built for working with strings.

提交回复
热议问题