PHP Warning: pack(): Type H: illegal hex digit r error [duplicate]
Asked Answered
A

1

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!

Ambiguity answered 16/12, 2012 at 22:14 Comment(0)
O
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;
}
Otho answered 17/12, 2012 at 0:16 Comment(3)
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.Ambiguity
Great stuff. Glad to hear you found it yourself.Otho
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.Loiret

© 2022 - 2024 — McMap. All rights reserved.