PHP | bin2hex() Function


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.

Syntax:

bin2hex($string)

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.



Examples:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate
// the bin2hex() fucntion 
  
$str = "geeks";
  
echo bin2hex($str);
  
?>

chevron_right


Output:

6765656b73

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate 
// the bin2hex() fucntion 
  
$str = "1111";
  
echo bin2hex($str);
  
?>

chevron_right


Output:

31313131 

Reference:
http://php.net/manual/en/function.bin2hex.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.