使用PHP将二进制转换为十六进制 [英] Convert binary to hexadecimal using PHP

查看:628
本文介绍了使用PHP将二进制转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP将二进制数(即 1111111 )转换为十六进制(即 7f )?我承认我可以做 dechex(bindec('1111111')); ,但是,我确定这不是正确的做法。

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'));, however, I am certain that this is not the right way.

我尝试了 bin2hex('1111111'),但它产生了31313131313131。

I tried bin2hex('1111111') but it resulted in 31313131313131.

推荐答案

您的解决方案很好。您也可以使用 base_convert

Your solution is fine. You can also use base_convert.

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

但请记住,php不是为计算而构建的。它是为使用字符串而构建的。

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

这篇关于使用PHP将二进制转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆