PHP十进制二进制转换,而无需decbin() [英] PHP decimal to binary conversion without decbin()

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

问题描述

我试图让十进制二进制转换器不使用decbin()函数。我想这样的不便(这是确定的,但它需要被逆转):

I'm trying to make decimal to binary convertor without using decbin() function. I tried smth like this (which is ok but it needs to be reversed):

    $dec=101;
    while ($dec>=1){
    $bin = $dec % 2;
    $dec = round($dec/2, 0, PHP_ROUND_HALF_DOWN);
    print "$bin";
    }// output:1010011

我怎么能扭转输出到1100101?
谢谢前进。 :)

How could I reverse the output to 1100101? Thanks in forward. :)

推荐答案

有是扭转一个字符串的 strrev

There is a function to reverse a string strrev

$dec=101;
$binStr = '';
while ($dec>=1){
    $bin = $dec % 2;
    $dec = round($dec/2, 0, PHP_ROUND_HALF_DOWN);
    $binStr .= $bin;
}
$binStr = strrev($binStr);
echo $binStr;

这篇关于PHP十进制二进制转换,而无需decbin()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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