Home > Backend Development > PHP Problem > How to convert binary to hex string in php

How to convert binary to hex string in php

青灯夜游
Release: 2023-03-13 07:36:02
Original
3282 people have browsed it

php method to convert binary to hex string: 1. Use the bindec() and dechex() functions, the syntax is "dechex(bindec($unm))". 2. Use the base_convert() function, the syntax is "base_convert($unm, 2, 16)".

How to convert binary to hex string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php will convert binary hex (hexadecimal) string

Method 1: Use the bindec() and dechex() functions--decimal to make the intermediate amount

<?php
$a="11110";
$b=bindec($a);
$c=dechex($b);
echo $c;
?>
Copy after login

Output result:

1e
Copy after login
Copy after login

bindec() can convert binary numbers to decimal numbers.

dechex() function can convert decimal numbers to hexadecimal numbers.

Method 2: Use base_convert() function

<?php
$a="11110";
echo base_convert($a, 2, 16);
?>
Copy after login

Output result:

1e
Copy after login
Copy after login

base_convert(number, original base, to be converted base) function converts numbers between any bases.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert binary to hex string in php. For more information, please follow other related articles on the PHP Chinese website!

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