note 86752 deleted from function.bin2hex by danbrown
| From: | danbrown@php.net | Date: | Sun, 02 Nov 2008 14:49:57 +0000 |
| Subject: | note 86752 deleted from function.bin2hex by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146048@lists.php.net to get a copy of this message | ||
Note Submitter: Ripx
----
Thanks to chaos79
<?php
// Convert a hex-string to binary-string (the way back from bin2hex)
function hex2bin($h)
{
if (!is_string($h)) return null;
$long=strlen($h);
if($long%2 != 0) return null;
$r='';
for ($a=0; $a<$long; $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
return $r;
}
echo bin2hex('Hello'); // result: 48656c6c6f
echo hex2bin('48656c6c6f'); // result: Hello
?>