在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";
通过以上代码,您可以将数字在十进制、十六进制、二进制之间相互转换。