pack() in php. Illegal hex digit warning

前端 未结 7 1687
梦如初夏
梦如初夏 2021-01-12 02:34

i am having some problems using pack() in php

$currencypair = \"EUR/USD\";
$buy_sell = \"buy\";
$alert_device_token =array(\"a\",\"a\",\"b\");
$message = \"         


        
相关标签:
7条回答
  • 2021-01-12 02:44

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 02:50

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

    0 讨论(0)
  • 2021-01-12 02:53

    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.

    0 讨论(0)
  • 2021-01-12 02:55

    In this case, $alert_device is an array.

    For packing it needs a value.

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

    0 讨论(0)
  • 2021-01-12 02:59

    Try to save your file in utf-8 encoding.

    0 讨论(0)
提交回复
热议问题