When working with low-level data or network programming, we often encounter the need to convert between binary and hexadecimal formats. Fortunately, PHP provides simple and easy-to-use built-in functions to help us complete this task, with bindec() and base_convert() being the most commonly used.
bindec() is a built-in PHP function that converts a binary string into a decimal number. dechex(), on the other hand, converts a decimal number into a hexadecimal string. By combining these two functions, we can easily perform binary to hexadecimal conversion.
$binary = '11010110';
$decimal = bindec($binary); // Convert to decimal
$hex = dechex($decimal); // Convert to hexadecimal
echo strtoupper($hex); // Output: D6
In this example, 11010110 is first converted to decimal 214, and then converted to hexadecimal D6.
If you find converting to decimal and then to hexadecimal cumbersome, PHP’s base_convert() offers a more direct method. This function allows us to specify the source and target number bases, enabling quick base conversions.
$binary = '11010110';
$hex = base_convert($binary, 2, 16);
echo strtoupper($hex); // Output: D6
The first parameter of this function is the string to be converted, the second parameter is the original base (2 in this case), and the third parameter is the target base (16 here).
The binary string passed in must be a valid binary number (only containing 0s and 1s). Otherwise, both bindec() and base_convert() might return unexpected results.
When using base_convert(), the return value is of type string. If you need to perform further conversions between different bases, make sure to handle the type conversion.
If padding is required, you can use the str_pad() function. For example, to always output a 2-digit hexadecimal number, you can do:
$hex = strtoupper(str_pad(base_convert('1101', 2, 16), 2, '0', STR_PAD_LEFT)); // Output: 0D
This type of conversion is common when working with network data packets, color codes (such as RGB values), bitmap data, and other scenarios. For instance, when receiving a binary data stream, you might want to print it in hexadecimal format for debugging:
$data = ['11000011', '10101010', '11110000'];
foreach ($data as $binary) {
echo strtoupper(base_convert($binary, 2, 16)) . " ";
}
// Output: C3 AA F0
You can also use the result as a hexadecimal color code, for example:
$r = '11110000';
$g = '10101010';
$b = '11000011';
$hexColor = '#' . strtoupper(base_convert($r, 2, 16)) .
strtoupper(base_convert($g, 2, 16)) .
strtoupper(base_convert($b, 2, 16));
echo $hexColor; // Output: #F0AAC3
If you retrieve a binary-encoded data segment from a remote API, such as:
$response = file_get_contents('https://m66.net/api/data/binary');
$data = json_decode($response, true); // Assume it returns ['11001100', '11110000']
foreach ($data as $binary) {
echo strtoupper(base_convert($binary, 2, 16)) . " ";
}
// Output: CC F0
By combining file_get_contents(), json_decode(), and base_convert(), we can quickly convert binary data into a readable hexadecimal format for processing and display.