PHP hex2bin Function | Convert Hexadecimal to Binary String

PHP hex2bin is an inbuilt function in PHP. It converts a hexadecimal character sequence back to a binary string. However, it does not convert the hexadecimal string to binary number. In this article, we will discuss the PHP hex2bin Function. Also, we will discuss a few examples of using it.

Also, in order to convert a binary string to its corresponding hexadecimal character sequence, you can use the PHP bin2hex Function.

Remember: There is a very big difference between binary data and a string representation of binary.

Syntax

string hex2bin($hexadecimalString)

Parameters

The function expects only one parameter. The hexadecimal string which needs to be converted to a binary string is the input to the function.

Return Value

The function returns the corresponding binary string after converting the hexadecimal input. However, on failure, it returns false.

Input: hex2bin('636f6e6361746c79')
Output: concatly
PHP hex2bin Function

Example

For instance, consider the following example of using the function.

<?php
	
	$hexadecimalString = '636f6e6361746c79';
	$binaryString = hex2bin($hexadecimalString);
	echo $binaryString;

?>

In the above example, the function converts the input hexadecimal string to a binary string. However, the function cannot convert the string to a binary number.

OUTPUT:
concatly

Conclusion

In conclusion, we discussed the PHP hex2bin Function. Also, you can read more about it on the Official PHP Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *