Latest web development tutorials
 

PHP bin2hex() Function

< PHP String Reference

Example

Convert "Hello World!" to hexadecimal values:

<?php
$str = bin2hex("Hello World!");
echo($str);
?>
Run example »

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 converted string
PHP Version: 4+

More Examples

Example 1

Convert a string value from binary to hex and back:

<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
Run example »

< PHP String Reference