11

i am having some problems using pack() in php

$currencypair = "EUR/USD";
$buy_sell = "buy";
$alert_device_token =array("a","a","b");
$message = "Your " . $currencypair . " " . $buy_sell . " alert price has been reached!";
$payload['aps'] = array (
  'alert' => $message,
  'badge' => 1,
  'sound' => 'default'
);
$payload = json_encode($payload);

foreach ($alert_device_token as $alert_device)
{
  $apnsMessage = chr(0) . chr(0) . chr(32) . 
                 pack('H*', str_replace(' ', '', $alert_device)) . 
                 chr(0) . chr(strlen($payload)) . $payload;
  echo $apnsMessage;
}

Now sometimes i get following warnings running the same code -

Warning: pack() [function.pack]: Type H: illegal hex digit g in /code/FR2BVl

the illegal hex digit keeps varying though. Any ideas about this warning and ways to remove it.

check it live here

Robbert
  • 6,481
  • 5
  • 35
  • 61
ayush
  • 14,350
  • 11
  • 53
  • 100
  • Post more code. The `str_replace()` on `$alert_device` makes me think that there is more code in the middle that could be modifying the contents of `$alert_device`. With the code as posted, you shouldn't get the error that you are getting. – Sander Marechal Jan 19 '11 at 07:30
  • In my case, I received this error when I was trying to pass an empty value (or a slash /) to the pack() function. I found out what was causing the empty value and fixed that. – Volomike Jan 25 '12 at 18:54

7 Answers7

8

pack converts hexadecimal number to binary, e.g.:

  echo pack("H*", "2133")

produces !3, since ! has code 0x21 and 3 has code 0x33. Since g is not hex digit, warning is given. To be useful for pack's H format, the argument must be hex number. If $alert_device isn't - you should use something else, depending on what it is and what you expect as the result.

StasM
  • 10,593
  • 6
  • 56
  • 103
6

One of the reason for the error is related to the checksums,

Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32() results will be positive integers though. So you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32() checksum in decimal format. http://www.php.net/crc32

To fix the error this might be sufficient,

sprintf('%u', CRC32($someString))

In this case,

pack('H*', str_replace(' ', '', sprintf('%u', CRC32($alert_device))))

Ref: https://github.com/bearsunday/BEAR.Package/issues/136

RunningAdithya
  • 1,656
  • 2
  • 16
  • 22
3

I was having the same issue when developing a hybrid app using Ionic/Cordova/PhoneGap. As the same code is run in Android and iOS devices, I had made a mistake of storing Google FCM token as APNS token. The APNS token is purely hexadecimal but Google FCM token can have non-hexadecimal characters. So, packing a Google FCM token using PHP's pack() function will result in the illegal hex digit error.

Nikunj Bhatt
  • 188
  • 4
  • 16
  • Thanks! If you want APN-token instead of FCM-token for iOS don't include`GoogleService-Info.plist` in the root directory, That was my mistake. – Tibbelit Jul 04 '18 at 10:01
2

Use strtr(rtrim(base64_encode(pack('H*', sprintf('%u', $algo($data)))), '='), '+/', '-_') insted of using pack('H*', $value).

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
1

In this case, $alert_device is an array.

For packing it needs a value.

Use pack('H*', str_replace(' ', '', $alert_device[0])) instead.

Yogesh lele
  • 392
  • 4
  • 17
1

You must change

pack('H*', $someString)

To

strtr(rtrim(base64_encode(pack('H*', sprintf('%u', CRC32($someString))))
Zulu
  • 8,765
  • 9
  • 49
  • 56
Nageswara Rao
  • 11
  • 1
  • 1
-3

Try to save your file in utf-8 encoding.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
chings228
  • 1,859
  • 24
  • 24