Current Location: Home> Latest Articles> Use hexdec + bin2hex to convert binary and decimal

Use hexdec + bin2hex to convert binary and decimal

gitbox 2025-05-27

Data conversion is a very common operation in daily PHP programming, especially when dealing with underlying communications, encryption algorithms, or data parsing, which often require conversion between binary, hexadecimal and decimal. This article will teach you how to achieve efficient conversion from binary to decimal through two PHP native functions, hexdec() and bin2hex() .

Why not use bindec directly?

PHP provides the bindec() function to directly convert binary strings into decimal numbers. So why do we still need to use hexdec() and bin2hex() ?

The answer is: greater flexibility and compatibility.

  • bindec() is only suitable for pure strings composed of 0 and 1 , and is prone to errors when dealing with large numbers or binary raw data such as byte streams.

  • bin2hex() and hexdec() can process raw binary strings , which is more stable and reliable when handling low-level data packets, encryption or file format parsing.

Basic usage explanation

1. bin2hex() – binary to hexadecimal

The function of this function is to convert a binary string (note that it is a binary string in PHP, not a string represented by 0 and 1 ) into a hexadecimal representation:

 $data = "\x0A"; // Decimal is 10 Bytes of
$hex = bin2hex($data);
echo $hex; // Output: 0a

2. hexdec() – Hex to decimal

Convert a hexadecimal string to a decimal integer:

 $hex = "0a";
$decimal = hexdec($hex);
echo $decimal; // Output: 10

Combined use to implement binary to decimal

Suppose we read a binary byte stream from some API or underlying file, and we want to convert it to a decimal value. The code is as follows:

 $binary = "\x01\x02"; // This is two bytes: 00000001 00000010

$hex = bin2hex($binary);       // Output: 0102
$decimal = hexdec($hex);       // Output: 258

echo "The decimal value is: " . $decimal;

The "\x01\x02" here is actually equivalent to binary 00000001 00000010 , and after combination it is hexadecimal 0102 , which is converted to decimal exactly 258 .

A real use scenario

Suppose you are developing an interface that communicates with a hardware device that returns 2 bytes to you through a Socket to indicate temperature, and the original data you receive is as follows:

 $socketData = "\x00\x7D"; // This means hexadecimal 007D

You want to convert it to a decimal value to display the temperature reading:

 $hex = bin2hex($socketData);   // Output: 007d
$temperature = hexdec($hex);   // Output: 125

echo "The current temperature is: " . $temperature . "°C";

Additional: How to deal with big-endian and small-endian formats?

If the data you receive is encoded in Little Endian (low bytes are in front), you need to reverse the byte order first:

 $littleEndian = "\x7D\x00";
$bigEndian = strrev($littleEndian); // Turn to big end order
$decimal = hexdec(bin2hex($bigEndian)); // Correctly parsed as 125

echo $decimal; // Output: 125

summary

bin2hex() and hexdec() are very important tools for processing binary data in PHP. They perform particularly well in processing file flow, underlying protocols, data parsing and other scenarios. Although bindec() is more suitable for handling simple binary strings, the combination of bin2hex() and hexdec() is undoubtedly more powerful and more stable when facing more complex data formats.

If you want to learn these data processing techniques in depth, you can visit:

<code> https://gitbox.net/php/hexdec-bin2hex-guide </code>

Master these underlying skills to make your PHP programming more professional and efficient!