Search for:


PHP bin2hex Function

The bin2hex() function is a built-in function in PHP that converts binary data to its hexadecimal representation. It takes a binary string as input and returns the corresponding hexadecimal string.

Here is the syntax of the bin2hex() function:

string bin2hex ( string $binary_string )

The bin2hex() function takes one argument, which is the binary string to be converted to hexadecimal. The function returns the hexadecimal string.


$binary_string = "Hello World!";
$hex_string = bin2hex($binary_string);
echo $hex_string;



In the above example, the bin2hex() function converts the binary string “Hello World!” to its hexadecimal representation and stores the result in the $hex_string variable. The echo statement displays the hexadecimal string.

Note that the input binary string must have an even number of characters. If it has an odd number of characters, the bin2hex() function will append a “0” to the beginning of the string to make it even.