Home>Article>Backend Development> How to perform base conversion in php
Conversion method: 1. Use bindec() to convert binary to decimal; 2. Use decbin() to convert decimal to binary; 3. Use octdec() to convert octal to decimal ; 4. Use decoct() to convert decimal to octal; 5. Use hexdec() to convert hexadecimal to decimal.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php for hexadecimal system Conversion
1.bindec--binary number to decimal number
You can use thebindec (binary string)
function , which converts binary numbers into decimal numbers.
"; echo bindec("01") . "
"; echo bindec("11000110011") . "
"; echo bindec("111"); ?>
Output result:
3 1 1587 7
2. decbin--decimal number to binary number
You can usedecbin(decimal value)
Function that converts decimal numbers to binary numbers.
"; echo decbin("1") . "
"; echo decbin("1587") . "
"; echo decbin("7"); ?>
Output result:
11 1 11000110011 111
3. octdec--octal number to decimal number
You can useoctdec (octal string)
Function, which converts octal numbers to decimal numbers.
"; echo octdec("12") . "
"; echo octdec("3063") . "
"; echo octdec("106"); ?>
Output result:
30 10 1587 70
4. decoct--convert decimal number to octal number
You can usedecoct (decimal value)
Function, which converts decimal numbers to octal numbers.
"; echo decoct("10") . "
"; echo decoct("1587") . "
"; echo decoct("70"); ?>
Output result:
36 12 3063 106
5. hexdec--convert hexadecimal number to decimal number
You can usehexdec(ten Hexadecimal string)
Function, which converts hexadecimal numbers to decimal numbers.
"; echo hexdec("a") . "
"; echo hexdec("11ff") . "
"; echo hexdec("cceeff"); ?>
Output result:
30 10 4607 13430527
6, dechex--decimal number to hexadecimal number
You can usedechex (decimal value)
Function that converts decimal numbers to hexadecimal numbers.
"; echo dechex("10") . "
"; echo dechex("1587") . "
"; echo dechex("70"); ?>
Output result:
1e a 633 46
7, base_convert--arbitrary base conversion
Usebase_convert (the number or character to be converted String, original base, base to be converted)
function, it can convert between any bases
Just set "bindec(binary string, 2, 10)
"Convert binary number to decimal number
"; echo base_convert("01",2,10) . "
"; echo base_convert("11000110011",2,10) . "
"; echo base_convert("111",2,10); ?>
Output result:
Set "base_convert(decimal value, 10, 2)
" can be converted from decimal number to binary number
"; echo base_convert("1",10,2) . "
"; echo base_convert("1587",10,2) . "
"; echo base_convert("7",10,2); ?>
Output result:
Set "base_convert(octal string, 8, 10 )
" can convert an octal number to a decimal number
"; echo base_convert("12", 8, 10) . "
"; echo base_convert("3063", 8, 10) . "
"; echo base_convert("106", 8, 10); ?>
Output result:
Set "base_convert(decimal value, 10, 8 )
" to convert decimal number to octal number
"; echo base_convert("10", 10, 8) . "
"; echo base_convert("1587", 10, 8) . "
"; echo base_convert("70", 10, 8); ?>
Output result:
Set "base_convert(hexadecimal string , 16, 10)
" that is, convert hexadecimal number to decimal number
"; echo base_convert("a", 16, 10) . "
"; echo base_convert("11ff", 16, 10) . "
"; echo base_convert("cceeff", 16, 10); ?>
Output result:
Set "base_convert (Decimal value, 10, 16)
” can be converted from decimal number to hexadecimal number
"; echo base_convert("10", 10, 16) . "
"; echo base_convert("1587", 10, 16) . "
"; echo base_convert("70", 10, 16); ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to perform base conversion in php. For more information, please follow other related articles on the PHP Chinese website!