在PHP中,可以使用bcmath擴展中的函數來實現進制轉換。以下是一個示例代碼:
// 將十進制轉換為十六進制
$decimalNumber = '123';
$hexNumber = bcdechex($decimalNumber);
echo "Decimal number $decimalNumber is equal to hex number $hexNumber\n";
// 將十六進制轉換為十進制
$hexNumber = '7B';
$decimalNumber = hexdec($hexNumber);
echo "Hex number $hexNumber is equal to decimal number $decimalNumber\n";
// 將二進制轉換為十進制
$binaryNumber = '1010101';
$decimalNumber = bindec($binaryNumber);
echo "Binary number $binaryNumber is equal to decimal number $decimalNumber\n";
// 將十進制轉換為二進制
$decimalNumber = '42';
$binaryNumber = decbin($decimalNumber);
echo "Decimal number $decimalNumber is equal to binary number $binaryNumber\n";
通過以上代碼,您可以將數字在十進制、十六進制、二進制之間相互轉換。