The `bin2hex()` string function in PHP 8.4 converts binary data into its hexadecimal representation. This is particularly useful when you need to encode binary data into a readable format, such as when preparing data for transmission or storage.
Syntax:<?phpbin2hex(string $string): string?>
$string: The input string containing binary data.
Return Value:The function returns a string containing the hexadecimal representation of the input binary data.
Example:<?php// Original binary data$binaryData = "Hello, World!";// Convert binary data to hexadecimal$hexadecimal = bin2hex($binaryData);// Display the hexadecimal representationecho $hexadecimal; // Outputs: 48656c6c6f2c20576f726c6421// To convert back from hexadecimal to binary$originalData = hex2bin($hexadecimal);// Display the original dataecho $originalData; // Outputs: Hello, World!?>
In this example, the string "Hello, World!" is converted to its hexadecimal equivalent, resulting in "48656c6c6f2c20576f726c6421". Using the `hex2bin()` function, we can revert the hexadecimal string back to its original binary form.