PHP Warning: pack(): Type H: illegal hex digit r error

Jon Erickson picture Jon Erickson · Dec 16, 2012 · Viewed 11.2k times · Source

Possible Duplicate:
pack() in php. Illegal hex digit warning

I am utilizing apple's push notification service and in order to send the notification, you have to build the message in binary. I got the following errors for the line below:

Warning: pack(): Type H: illegal hex digit r

Warning: pack(): Type H: illegal hex digit y

Notice: Array to string conversion in C:\xampp\htdocs\firecom\FireComAPNS.php on line 130

Here's the line of code throwing the error:

$msg = chr(0).pack('n', 32).pack('H*', $devicetoken).pack('n',strlen($payload)) . $payload;

and

$devicetoken=773f5436825a7115417d3d1e036da20e806efeef547b7c3fe4da724d97c01b30

I have searched on the internet a lot, but I have no idea how to mess with binary, any help on what's going on would be greatly appreciated!

Answer

Glenn Plas picture Glenn Plas · Dec 17, 2012

Try this function for php < 5.4.0

function hex2bin($hexdata) {
   $bindata="";
   for ($i=0;$i<strlen($hexdata);$i+=2) {
      $bindata.=chr(hexdec(substr($hexdata,$i,2)));
   }

   return $bindata;
}