我的目标是将十进制整数转换为二进制,如本视频中所述,http://youtu.be/XdZqk8BXPwg 使用php函数,我知道php可以使用内置的decbin()开箱即用,但无论如何我都想写一个。
<?php
function decToBin($int) {
$roundInt = intval($int) * 2;
while ($roundInt > 1) {
$result = intval($roundInt = $roundInt / 2);
if ($result % 2 == 0) {
$result = 0;
} else {
$result = 1;
}
echo $result;
}
}
decToBin(123);
我尝试在循环时,但我得到的结果颠倒了。
有没有办法我可以反转,所以不是11011110我得到01111011,或者没有前面的零更好。
谢谢
慕的地8271018
鸿蒙传说
繁星点点滴滴