# How to use PHP functions for binary conversion

Keywords: PHP Back-end

In the previous article "using PHP function to return the day before and the day after a date", we introduced the methods of using strtotime() function to obtain the day before and the day after a given date, the date of the previous month and the next month, the date of a week and the date of the next week. Interested friends can learn about it~

The focus of this paper is "base conversion", which introduces the mutual conversion of binary number and decimal number, the mutual conversion of decimal number and hexadecimal number, and the mutual conversion of decimal number and octal number.

Conversion between binary and decimal numbers

1. Binary to decimal

have access to   Bindec (binary string)   Function, which converts a binary number to a decimal number.

```<?php
echo bindec("0011") . "<br>";
echo bindec("01") . "<br>";
echo bindec("11000110011") . "<br>";
echo bindec("111");
?>```

Output results:

```3
1
1587
7```

Can also be used   base_ Convert (number or string to be converted, original base, base to be converted)   Function, which can be converted between any hexadecimal, just set "bindec (binary string, 2, 10)"

```<?php
echo base_convert("0011",2,10) . "<br>";
echo base_convert("01",2,10) . "<br>";
echo base_convert("11000110011",2,10) . "<br>";
echo base_convert("111",2,10);
?>```

Output results:

```3
1
1587
7```

2. Decimal to binary

You can use decbin (decimal value)   Function that converts a decimal number to a binary number.

```<?php
echo decbin("3") . "<br>";
echo decbin("1") . "<br>";
echo decbin("1587") . "<br>";
echo decbin("7");
?>```

Output results:

```11
1
11000110011
111```

You can also use base_convert() function, just set "bind C (decimal value, 10, 2)".

```<?php
echo base_convert("3",10,2) . "<br>";
echo base_convert("1",10,2) . "<br>";
echo base_convert("1587",10,2) . "<br>";
echo base_convert("7",10,2);
?>```

Output results:

```11
1
11000110011
111```

Conversion between decimal and octal numbers

1. Octal to decimal

have access to   Octdec (octal string)   Function, which converts an octal number to a decimal number.

```<?php
echo octdec("36") . "<br>";
echo octdec("12") . "<br>";
echo octdec("3063") . "<br>";
echo octdec("106");
?>```

Output results:

```30
10
1587
70```

You can also use base_convert() function, just set "bind C (octal string, 8, 10)".

```<?php
echo base_convert("36", 8, 10) . "<br>";
echo base_convert("12", 8, 10) . "<br>";
echo base_convert("3063", 8, 10) . "<br>";
echo base_convert("106", 8, 10);
?>```

Output results:

```30
10
1587
70```

2. Decimal to octal

have access to   Decoct (decimal value)   Function, which converts a decimal number to an octal number.

```<?php
echo decoct("30") . "<br>";
echo decoct("10") . "<br>";
echo decoct("1587") . "<br>";
echo decoct("70");
?>```

Output results:

```36
12
3063
106```

You can also use base_convert() function, just set "bind C (decimal value, 10, 8)".

```<?php
echo base_convert("30", 10, 8) . "<br>";
echo base_convert("10", 10, 8) . "<br>";
echo base_convert("1587", 10, 8) . "<br>";
echo base_convert("70", 10, 8);
?>```

Output results:

```36
12
3063
106```

Conversion between decimal and hexadecimal numbers

```<?php
echo hexdec("1e") . "<br>";
echo hexdec("a") . "<br>";
echo hexdec("11ff") . "<br>";
echo hexdec("cceeff");
?>```

Output results:

```30
10
4607
13430527```

You can also use base_convert() function, just set "bind C (hexadecimal string, 16, 10)".

```<?php
echo base_convert("1e", 16, 10) . "<br>";
echo base_convert("a", 16, 10) . "<br>";
echo base_convert("11ff", 16, 10) . "<br>";
echo base_convert("cceeff", 16, 10);
?>```

Output results:

```30
10
4607
13430527```

```<?php
echo dechex("30") . "<br>";
echo dechex("10") . "<br>";
echo dechex("1587") . "<br>";
echo dechex("70");
?>```

Output results:

```1e
a
633
46```

You can also use base_convert() function, just set "bind C (decimal value, 10, 16)".

```<?php
echo base_convert("30", 10, 16) . "<br>";
echo base_convert("10", 10, 16) . "<br>";
echo base_convert("1587", 10, 16) . "<br>";
echo base_convert("70", 10, 16);
?>```

Output results:

```1e
a
633
46```

That's it

http://www.e6uu.com/sitemap.xml

Posted by clintonium on Thu, 21 Oct 2021 17:37:33 -0700