The bin2hex() function converts a string of binary to hexadecimal values.
bin2hex() Function has the following syntax.
bin2hex(string)
Parameter | Is Reqired | Description |
---|---|---|
string | Required. | The string to be converted |
PHP bin2hex() function returns the hexadecimal string.
Convert binary to hexadecimal values:
<?php
$str = bin2hex("11111001");
echo($str);
?>
The code above generates the following result.
The following code shows how to convert a string value from binary to hex and back.
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
The code above generates the following result.