7

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!

Community
  • 1
  • 1
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

1 Answers1

0

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;
}
Glenn Plas
  • 1,608
  • 15
  • 17
  • 4
    I figured out the problem. $deviceToken was an array instead of a string. I used $deviceToken = $row['devicetoken']; to get the right row and all is well again. – Jon Erickson Dec 17 '12 at 01:37
  • Great stuff. Glad to hear you found it yourself. – Glenn Plas Dec 17 '12 at 08:58
  • That great that you figured it out, but it's more important to realize that 1) the array you passed was being converted to a string by `pack`; 2) the string value was the text `Array`; 3) The `r` and `y` characters in that string are not valid hex digits; 4) ...which is why it failed. – Wayne Nov 04 '13 at 17:50