How to convert binary to hexadecimal string in php

青灯夜游
Release: 2023-03-15 06:00:01
Original
2638 people have browsed it

php method to convert binary to hexadecimal string: 1. Use base_convert() function, syntax "base_convert(binary number, 2,16)"; 2. Use bindec() and dechex() Function, syntax "dechex(bindec(binary number))".

How to convert binary to hexadecimal string in php

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

php converts binary to 16 Method of converting strings:

Method 1: Use base_convert() function

base_convert() function to convert numbers between arbitrary bases.

base_convert($binary_string,$frombase,$tobase);
Copy after login
  • $binary_string Required. Specifies the binary 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.

Example:

<?php
header("content-type:text/html;charset=utf-8");
$num1="0011";
$num2="11000110011";
echo "二进制 ".$num1." 转换成十六进制 ".base_convert($num1,2,16)."<br>"; 
echo "二进制 ".$num2." 转换成十六进制 ".base_convert($num2,2,16)."<br>"; 
?>
Copy after login

How to convert binary to hexadecimal string in php

2. Use bindec() dechex() function

bindec($binary_string) Function converts binary number $binary_string to decimal number.

dechex($number) The function converts the decimal number $number into a hexadecimal string.

<?php
header("content-type:text/html;charset=utf-8");
$num1="0011";
$num2="11000110011";
echo "二进制 ".$num1." 转换成十六进制 ".dechex(bindec($num1))."<br>"; 
echo "二进制 ".$num2." 转换成十六进制 ".dechex(bindec($num2))."<br>"; 
?>
Copy after login

How to convert binary to hexadecimal string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert binary to hexadecimal string 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!