Why we use bin2hex() Function in PHP
Why we use bin2hex() Function in PHP
The bin2hex() function is used to converts a string to hexadecimal. This conversion is performed byte-wise and with the high-nibble first.
Note: It is not for converting strings representing binary digits into hexadecimal.
Syntax:
bin2hex($my_string)
What are the Parameters?
The bin2hex($my_string) function accepts a single parameter $my_string. This is the string that will be converted to hexadecimal values.
What are Return Values?
The bin2hex($my_string) function returns the hexadecimal value of the string passed in the parameter.
Examples:
Input : string = ” t4tutorials”
Output : 74347475746f7269616c73
Input : string = ” 444″
Output : 343434
Let’s see some PHP programs that illustrate the bin2hex() function.
Example 1: PHP bin2hex() function
1 2 3 4 5 6 | <?php // PHP program to demonstrate // the bin2hex() fucntion $str = "t4tutorials"; echo bin2hex($str); ?> |
Output
74347475746f7269616c73
Example 2: PHP bin2hex() function
1 2 3 4 5 6 | <?php // PHP program to demonstrate // the bin2hex() fucntion $str = "444"; echo bin2hex($str); ?> |
Output
343434