Daily Archives: 2013/04/14

hex2bin for PHP

在php5.4中, 已经有了hex2bin函数, 可以快速的将hex转为二进制.而在低于5.4的版本中如何处理? 可以通过pack进行转换

function hex2bin($hexstr) {
$n = strlen($hexstr);
$sbin=””;
$i=0;
while($i<$n){ $a =substr($hexstr,$i,2); $c = pack("H*",$a); if ($i==0){ $sbin=$c; } else { $sbin.=$c; } $i+=2; } return $sbin; } [/php]