In this article, you will learn;
- How to convert the ASCII characters to the Hexadecimal values.
The PHP bin2hex() method converts the ASCII characters into hexadecimal values. We can convert back the hexadecimal value to the ASCII character using the pack() function in PHP.
What is the BIN2HEX() syntax of the function in PHP?
bin2hex(string)
Parameter | Description |
---|---|
string | The String to convert to hexadecimal – Required |
Example of the BIN2HEX() function
Example 1. In this example, we simply pass the string to the bin2hex() function and then print the output.
<?php
$str = bin2hex("Hello World!");
echo($str);
?>
Example 2. In this example, we convert the string to a hexadecimal value and then convert it back to the ASCII string or character.
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>