How to convert hexadecimal representation of data to binary data in PHP?

If you look at PHP's bin2hex page, there's suggested solutions including this one:

$foo = pack("H*" , $foo);
echo $foo;

There's also various implementations of hex2bin that you can choose from.


For those who have PHP 5.4 and above, there's a standard way of doing this:

<?php $bin = hex2bin("6578616d706c65206865782064617461"); var_dump($bin); ?>

The output of the code above should be similar to:

string(16) "example hex data"

Gotten off of the PHP hex2bin page.


Try pack("H*",$foo).

http://us3.php.net/manual/en/function.pack.php

Tags:

Php

String