How to decode a hexadecimal string in PHP

The hex2bin method in PHP can be used to convert a hexadecimally encoded string to a binary string. However, this method doesn’t convert a hexadecimal number to a binary number.

Syntax

hex2bin(string $string): string|false

Parameters

hex2bin takes the hex string that will be converted to a binary string as a parameter.

Return value

The hex2bin method returns a string upon success; otherwise, it returns false.

Code

In this example, we are going to decode the encoded string Hello, educative!.

<?php
echo hex2bin("48656c6c6f2c2065647563617469766521");