Home >Backend Development >PHP Problem >How to convert data into binary in php
How to convert binary: 1. Use the decbin() function to convert decimal to binary, the syntax is "decbin("decimal value")"; 2. Use the base_convert() function to convert any To convert other bases to binary, the syntax is "base_convert("data value", original base value, 2)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Binary Conversion between numbers and decimal numbers
##1, Convert decimal numbers to binary numbers
You can use decbin(decimal value) function, which converts decimal numbers into binary numbers.
<?php echo decbin("3") . "<br>"; echo decbin("1") . "<br>"; echo decbin("1587") . "<br>"; echo decbin("7"); ?>Output result:
#2. Convert any other base to binary number
You can use base_convert () function converts numbers between arbitrary bases. Syntax:base_convert(number,frombase,tobase);
Description | |
---|---|
number | Required. Specifies the number to be converted.|
frombase | Required. Specifies the original base of the number. 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.|
tobase | Required. Specifies the base to be converted. 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.
<?php echo base_convert("3",10,2) . "<br>"; echo base_convert("ff",16,2) . "<br>"; echo base_convert("2003001",17,2) . "<br>"; echo base_convert("JZG8X",36,2); ?>Output Result: Recommended study: "
PHP Video Tutorial"
The above is the detailed content of How to convert data into binary in php. For more information, please follow other related articles on the PHP Chinese website!