PHP bin2hex() Function

Example

Convert "Shanghai" to hexadecimal value:

<?php
$str = bin2hex("Shanghai");
echo($str);
?>

Run Instance

Definition and Usage

The bin2hex() function converts a string of ASCII characters to hexadecimal values. The string can be converted back using the pack() function.

Syntax

bin2hex(string)
Parameter Description
string Required. The string to be converted.

Technical Details

Return Value: Returns the hexadecimal value of the string to be converted.
PHP Version: 4+

More Examples

Example 1

Convert a string value from binary to hexadecimal and back:

<?php
$str = "Shanghai";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>

Run Instance