2
A
回答
3
function func($number) {
return str_split(decbin($number));
}
5
decbin
會產生一個字符串二進制字符串:
echo decbin(14); # outputs "1110"
array_map('intval', str_split(decbin(14))) # acomplishes the full conversion
1
<?php
function int_to_bitarray($int)
{
if (!is_int($int))
{
throw new Exception("Not integer");
}
return str_split(decbin($int));
}
$result = int_to_bitarray(14);
print_r($result);
輸出:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 0
)
1
你可以繼續將其除以2並存儲剩餘反...
number = 14
14%2 = 0數= 14/2 = 7
7%2 = 1數= 7/2 = 3
3%2 = 1號= 3/2 = 1
1%2 = 1號= 1/2 = 0
1
for($i = 4; $i > 0; $i++){
array[4-$i] = (int)($x/pow(2,$i);
$x -= (int)($x/pow(2,$i);
}
...這將達到目的。在此之前,您可以檢查數組的大小以及$ i開始的值。
相關問題
- 1. 將十進制數位數組轉換爲二進制數位數組
- 2. 將十進制數字轉換爲6位二進制數字
- 3. 如何將數字(十進制)轉換爲二進制(二進制)數字和從二進制到十進制?
- 4. 如何將數組編碼爲二進制並將二進制數據解組到二進制數組中?
- 5. 如何將十進制數組轉換爲二進制數的數組?
- 6. 如何將十六進制NSData轉換爲十進制數組?
- 7. 將十進制浮點數轉換爲二進制並返回
- 8. 如何將64位十六進制數轉換爲Scala中的二進制數?
- 9. 將二進制向量轉換爲十進制數組
- 10. 使用數組將十進制轉換爲二進制
- 11. 使用數組將二進制轉換爲十進制
- 12. 將C十進制轉換爲無數組的二進制
- 13. 如何將int數從十進制轉換爲二進制
- 14. 如何將二進制轉換爲十進制與長整數?
- 15. 如何將十進制轉換爲二進制將其與〜顛倒並將其轉換回十進制
- 16. 將十進制數轉換爲二進制/十六進制/八進制符號
- 17. 將十進制數轉換爲二進制數
- 18. 將十進制數轉換爲二進制數
- 19. 將整數轉換爲二進制/十進制的MIPS函數?
- 20. 如何將8位十六進制值解釋爲無符號十進制數?
- 21. Perl:創建一個二進制數並將其轉換爲十六進制
- 22. 如何將十進制轉換爲二進制的64位?
- 23. 將二進制位轉換爲十六進制值
- 24. 將二進制轉換爲十進制
- 25. 將十進制轉換爲二進制
- 26. 將八進制數轉換爲十進制和十六進制
- 27. 將十進制數組轉換爲biginteger
- 28. 二進制十進制負數位集
- 29. 如何將十六進制文件轉換爲二進制數組Android,Java
- 30. python十進制數組轉換爲二進制數efficienty