Pack () in php. Invalid hexadecimal warning

I'm having 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 the following warnings that run the same code -

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

illegal hexadecimal digit continues to change. Any ideas on this warning and how to remove it.

check it out live here

+7
source share
7 answers

pack converts a hexadecimal number to binary, for example:

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

creates !3 since ! has the code 0x21 and 3 has the code 0x33. Since g not a hexadecimal digit, a warning is given. To be useful for pack H , the argument must be hexadecimal. If $alert_device not there, then you should use something else, depending on what it is and what you expect as a result.

+6
source

One reason for the error is with checksums,

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

To correct a mistake, this may be enough

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

In this case

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

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

+6
source

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

+2
source

You have to change

 pack('H*', $someString) 

For

 strtr(rtrim(base64_encode(pack('H*', sprintf('%u', CRC32($someString)))) 
+1
source

In this case, $alert_device is an array.

Packaging needs value.

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

0
source

I had the same problem when developing a hybrid application using Ionic / Cordova / PhoneGap. Since the same kayd was launched on Android and iOS devices, I made a mistake in storing the Google FCM token as an APNS token. The APNS token is hexadecimal, but the Google FCM token may have non-hexadecimal characters. Thus, packaging the Google FCM token using the PHP pack() function will result in an illegal hex digit error.

0
source

Try to save the file in utf-8 encoding.

-2
source

All Articles