Home >Backend Development >PHP Problem >How to convert data into binary in php

How to convert data into binary in php

青灯夜游
青灯夜游Original
2022-04-13 20:03:084174browse

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)".

How to convert data into binary in php

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:

How to convert data into binary in php

#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);

ParametersDescriptionRequired. Specifies the number to be converted. 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. 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.
number
frombase
tobase
You only need to set the parameter tobase to 2 to convert any other base number into a binary number

Example:

<?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:

How to convert data into binary in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn