The hex2bin() function is a built-in function in PHP. The hex2bin() function is used to decode a hexadecimally encoded binary string. That is, it decodes a binary string which is encoded in hexadecimal representation back to binary representation.
Note: The purpose of this function is not to convert a hexadecimal number to binary number, to do so we can use the base_convert() function in PHP. This function is just used to decode a hexadecimally encoded binary string.
Syntax:
string hex2bin($hexString)
Parameters: This function accepts a single parameter $hexString which represents a hexadecimal data which is needed to be decoded to binary representation.
Return Value: This function return a string which is the binary representation of the hexa-decimal input $hexString. If this function fails to decode the hexadecimal string to binary then it returns False.
Examples:
Input : $hexString = "48656c6c6f20576f726c6421" Output : Hello World! Input : $hexString = "6578616d706c65206865782064617461" Output : example hex data
Below programs illustrate the hex2bin() function:
Program 1:
<?php echo hex2bin("48656c6c6f20576f726c6421"); ?> |
Output:
Hello World!
Program 2:
<?php echo hex2bin("6578616d706c65206865782064617461"); ?> |
Output:
example hex data
Reference:
http://php.net/manual/en/function.hex2bin.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
- PHP | is_numeric() 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.

