Covenvert Hex to binary

Good morning,
Ive got a PHP script which needs to accept a binary file in HEX, and write it to disc, is there an easy and quick process for converting HEX to a byte and then writing it to a file?

The file is passed through to the PHP script through $_POST['fileContents'], and looks like:-

So I suppose first thing I need to do is split the string into individual bytes and then convert each hex number to its binary and then write that to disc, but my attached code isnt the quickest in the world, is there a way I can speed it up?
$hexArray = explode("-", $_POST['fileContent']);
$write2Disc = ""

foreach ($hexArray as $hexElement)
	$write2Disc = $write2Disc . chr(hexdec($hexElement));

$myFile = "testFile.jpg";
$fh = fopen($myFile, 'a');
fwrite($fh, $write2Disc);

Open in new window

Who is Participating?
shanikawmConnect With a Mentor Commented:
Try this.

$write2Disc = pack("H*",preg_replace('/-/','',$_POST['fileContent']))

Open in new window

You can use decbin() function to convert decimal to binary:
All Courses

From novice to tech pro — start learning today.