How to convert data into binary in php

青灯夜游
Release: 2023-03-15 17:58:01
Original
4121 people have browsed it

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");
?>
Copy after login

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);
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!