PHP Tutorial - PHP bin2hex() Function






Definition

The bin2hex() function converts a string of binary to hexadecimal values.

Syntax

bin2hex() Function has the following syntax.

bin2hex(string)

Parameter

ParameterIs Reqired Description
stringRequired. The string to be converted

Return

PHP bin2hex() function returns the hexadecimal string.

Example

Convert binary to hexadecimal values:


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

The code above generates the following result.





Example 2

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.