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:
<?php // PHP program to demonstrate // the bin2hex() fucntion $str = "geeks"; echo bin2hex($str); ?> |
Output:
6765656b73
Program 2:
<?php // PHP program to demonstrate // the bin2hex() fucntion $str = "1111"; echo bin2hex($str); ?> |
Output:
31313131
Reference:
http://php.net/manual/en/function.bin2hex.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | sin( ) Function
- D3.js | d3.max() function
- CSS | hsl() Function
- p5.js | hex() function
- p5.js | str() function
- PHP | cos( ) Function
- D3.js | d3.map.set() Function
- PHP Ds\Map sum() Function
- PHP | Ds\Map first() Function
- PHP | Ds\Map get() Function
- CSS | url() Function
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.

