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

How to convert binary to hex string in php

青灯夜游
青灯夜游Original
2021-09-30 14:50:563146browse

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

Output result:

1e

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

Output result:

1e

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!

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