Home > Article > Backend Development > php base conversion function
php base conversion function is as follows:
bindec() — Convert binary to decimal
decbin() — Convert decimal to binary
dechex() — Convert decimal to hexadecimal
decoct() — Convert decimal to octal
hexdec () — Convert hexadecimal to decimal
octdec() — Convert octal to decimal
base_convert() – Convert numbers between arbitrary base systems
The application is explained as follows:
One, decimal system conversion Function explanation
1, decimal to binary decbin() function, the following example
echo decbin(12); //Output 1100
echo decbin(26); //Output 11010
decbin
(PHP 3, PHP 4, PHP 5 )
decbin -- Convert decimal to binary
Explanation
string decbin (int number)
Returns a string containing the binary representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal, and the result is a string of 32 ones.
2, decimal to octal decoct() function
echo decoct(15); //Output 17
echo decoct(264); //Output 410
decoct
(PHP 3, PHP 4, PHP 5)
decoct - - Decimal to octal conversion
Explanation
string decoct (int number)
Returns a string containing the octal representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal, and the result is "37777777777".
3, decimal to hexadecimal dechex() function
echo dechex(10); //Output a
echo dechex(47); //Output 2f
dechex
(PHP 3, PHP 4, PHP 5)
dechex -- Convert decimal to hexadecimal
Explanation
string dechex (int number)
Returns a string containing the hexadecimal representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal, and the result is "ffffffff".
2. Explanation of binary system conversion function
1. Bin2hex() function from binary to hexadecimal
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $ hex;//Output f9
bin2hex
(PHP 3 = 3.0.9, PHP 4, PHP 5)
bin2hex -- Convert binary data to hexadecimal representation
Explanation
string bin2hex (string str)
Return ASCII characters String is the hexadecimal representation of parameter str. Conversion applies byte method, high nibble first.
2, Bindec() function to convert binary to decimal
echo bindec(\'110011\'); //Output 51
echo bindec(\'000110011\'); //Output 51
echo bindec(\' 111\'); //Output 7
bindec
(PHP 3, PHP 4, PHP 5)
bindec -- binary to decimal conversion
clarification
number bindec (string binary_string)
returns the decimal of the binary number represented by the binary_string parameter equal value.
bindec() Converts a binary number to an integer. The largest number that can be converted is 31 digits, or 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large values, in which case it returns a float type.
Three, octal system (octal system) conversion function explanation
Octal to decimal octdec() function
echo octdec(\'77\'); //Output 63
echo octdec(decoct(45)); //Output 45
octdec
(PHP 3, PHP 4, PHP 5)
octdec -- Convert octal to decimal
Explain
number octdec (string octal_string)
Returns the decimal equivalent of the octal number represented by the octal_string parameter. The largest value that can be converted is 17777777777 or 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.
IV. Explanation of hexadecimal (hexadecimal) conversion function
Hexadecimal to decimal hexdec() function
var_dump(hexdec("See"));
var_dump(hexdec("ee"));
/ / both print "int(238)"
var_dump(hexdec("that")); // print "int(10)"
var_dump(hexdec("a0")); // print "int(160)"
hexdec
(PHP 3, PHP 4, PHP 5)
hexdec -- Convert hexadecimal to decimal
Explain
number hexdec (string hex_string)
Return the hexadecimal number equivalent to the hex_string parameter Decimal. hexdec() Converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7ffffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.
hexdec() Converts all non-hexadecimal characters encountered to 0. This way, all zeros on the left are ignored, but zeros on the right are included in the value.
Five, arbitrary base_convert() function
$hexadecimal = \'A37334\';
echo base_convert($hexadecimal, 16, 2);//Output 101000110111001100110100
base_convert
(PHP 3 = 3.0.6, PHP 4, PHP 5)
base_convert -- at any enter Convert numbers between bases
Explanation
string base_convert (string number, int frombase, int tobase)
Returns a string containing the representation of number in tobase base. The base of number itself is specified by frombase. Both frombase and tobase can only be between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.