The PHP function explains the conversion functions of decimal binary octal and hexadecimal

  • 2020-05-10 17:47:45
  • OfStack

1,10 (decimal system) conversion function
The 1,10 to 2 decbin() function is shown in the following example

echo decbin (12); / / output 1100
echo decbin (26); / / output 11010
decbin
(PHP 3, PHP 4, PHP 5)
decbin -- convert from base 10 to base 2
instructions
string decbin ( int number )
Returns a 1 string containing a base 2 representation with the given number parameter. The maximum value that can be converted is 4294967295 in base 10, resulting in a string of 32 ones.

2, hexadecimal to hexadecimal decoct() function

echo decoct (15); / / output 17
echo decoct (264); / / output 410
decoct
(PHP 3, PHP 4, PHP 5)
decoct -- convert from base 10 to base 8
instructions
string decoct ( int number )
Returns a 1 string containing the hexadecimal representation of the given number parameter. The maximum value that can be converted is 4294967295 in base 10, and the result is "377777777 ".

3,10 to 106 dechex() function

echo dechex (10); / / output a
echo dechex (47); / / output 2 f
dechex
(PHP 3, PHP 4, PHP 5)
dechex -- convert from base 10 to base 106
instructions
string dechex ( int number )
Returns a 1 string containing a base 106 representation with the given number parameter. The maximum value that can be converted is 4294967295 in base 10, and the result is "ffffffff".

2,2 (binary system) conversion function
1,2 to 106, bin2hex()

$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex; / / output f9
bin2hex
(PHP 3 > = 3.0.9, PHP 4, PHP 5)
bin2hex -- converts base 2 data into a base 106 representation
instructions
string bin2hex ( string str )
Returns the ASCII string, a base 106 representation of the parameter str. The conversion USES bytes, with the high 4 bit bytes taking precedence.

2,2 to 10, bindec()

echo bindec (' 110011 '); / / output 51
echo bindec (' 000110011 '); / / output 51
echo bindec (' 111 '); / / output 7
bindec
(PHP 3, PHP 4, PHP 5)
bindec -- convert from base 2 to base 10
instructions
number bindec ( string binary_string )
Returns the base 10 equivalent value of the base 2 number represented by the binary_string parameter.
bindec() converts a base 2 number to integer. The largest number that can be converted is 31-bit 1 or 2147483647 in hexadecimal. Starting with PHP 4.1.0, this function can handle large values, in which case it returns the float type.

3,8 (octal system) conversion function
octdec() function from 8 to 10

echo octdec (' 77 '); / / output 63
echo octdec (decoct (45)); / / output 45
octdec
(PHP 3, PHP 4, PHP 5)
octdec -- convert from base 8 to base 10
instructions
number octdec ( string octal_string )
Returns the hexadecimal equivalent of the hexadecimal number represented by the octal_string parameter. The maximum value that can be converted is 17777777777 or 2147483647 in base 10. Starting with PHP 4.1.0, this function can handle large Numbers, in which case it returns the float type.

Base 106 (hexadecimal) conversion function description
The hexadecimal 106 to the hexadecimal 10 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 from base 106 to base 10
instructions
number hexdec ( string hex_string )
Returns the hexadecimal number equivalent to the hexadecimal number represented by the hex_string parameter. hexdec() converts a base 106 string into a base 10 number. The maximum value that can be converted is 7fffffff, which is 2147483647 in base 10. Starting with PHP 4.1.0, this function can handle large Numbers, in which case it returns the float type.
hexdec() replaces all non-106 characters encountered with zeros. Thus, all the zeros on the left are ignored, but the zeros on the right count as values.

5, any base conversion 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 -- converts Numbers between any base
instructions
string base_convert ( string number, int frombase, int tobase )
Returns a string containing number in base tobase. The base of number itself is specified by frombase. Both frombase and tobase can only be between 2 and 36 (including 2 and 36). Numbers above base 10 are represented by the letters a-z, such as a for 10, b for 11, and z for 35.

Here, PHP conversion function is mainly sorted out for easy development and search. Please refer to PHP manual for specific function description. Please pay attention to the next series on Chinese character encoding.

Related articles: