PHP bin2hex() Function
PHP bin2hex() function is used to convert string value of ASSCII characters to hexadecimal value.
Syntax:
string bin2hex ( string $str )
Parameter | Description | Required/Optional |
---|---|---|
String | The string to be converted | Required |
Example 1
<?php
$str ="Hello World!";
echo "Your ASCII character is: ".$str;
echo "<br>"."By using 'bin2hex()' Method your hexadecimal value is: ".bin2hex($str);
?>
Output
Your ASCII character is:Hello World!
By using 'bin2hex()' Method your hexadecimal value is: 48656c6c6f20576f726c6421
Example 2
<?php
$str = "Hello world!";
echo "Your Hexadecimal Value is ".bin2hex($str) . "<br>"
echo pack("H*",bin2hex($str)) . "<br>"
?>
Output
Your Hexadecimal Value is 48656c6c6f20776f726c6421
Hello world!
Example 3
<?php
$binary = "11111001";
echo "Your Binary Value is".$binary."<br>"
$hex = dechex(bindec($binary));
echo "Converted Binary to Hexadecimal : ".$hex;
>?
Output
Your Binary Value is11111001
Converted Binary to Hexadecimal : f9