Skip to content
Related Articles

Related Articles

PHP | bin2hex() Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 28 Aug, 2020

The bin2hex() function in PHP converts a string to hexadecimal values. The conversion is done byte-wise with the high-nibble first.

Note: It is not for converting strings representing binary digits into hexadecimal.



Parameters: This function accepts a single parameter $string. This is the string that will be converted to hexadecimal values.

Return Value: The function returns the hexadecimal value of the string passed in the parameter.


Input : string = "geeks"
Output : 6765656b73

Input : string = "1111"
Output : 31313131 
Explanation: "1111" is converted to its hexadecimal 
values, it is not treated as a binary string, else the 
answer would have been F which is not in this case.

Below programs illustrate the bin2hex() function in PHP:

Program 1:

// PHP program to demonstrate
// the bin2hex() function 
$str = "geeks";
echo bin2hex($str);



Program 2:

// PHP program to demonstrate 
// the bin2hex() function 
$str = "1111";
echo bin2hex($str);




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!