打包()在 php.ini 文件中。非法十六进制数字警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4732681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:11:09  来源:igfitidea点击:

pack() in php. Illegal hex digit warning

phphexpack

提问by ayush

i am having some problems using pack() in php

我在 php 中使用 pack() 时遇到了一些问题

$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

检查它住在这里

采纳答案by StasM

packconverts hexadecimal number to binary, e.g.:

pack将十六进制数转换为二进制数,例如:

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

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

产生!3,因为!代码为 0x21,3代码为 0x33。由于g不是十六进制数字,因此给出警告。为了对包的H格式有用,参数必须是十六进制数。如果$alert_device不是 - 您应该使用其他东西,具体取决于它是什么以及您期望的结果。

回答by RunningAdithya

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

因为 PHP 的整数类型是有符号的,所以在 32 位平台上,许多 crc32 校验和将导致负整数。不过,在 64 位安装上,所有 crc32() 结果都是正整数。因此,您需要使用 sprintf() 或 printf() 的“%u”格式化程序来获取十进制格式的无符号 crc32() 校验和的字符串表示形式。 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

参考:https: //github.com/bearsunday/BEAR.Package/issues/136

回答by Nikunj Bhatt

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 digiterror.

在使用 Ionic/Cordova/PhoneGap 开发混合应用程序时,我遇到了同样的问题。由于在 Android 和 iOS 设备上运行相同的代码,我犯了将 Google FCM 令牌存储为 APNS 令牌的错误。APNS 令牌是纯十六进制的,但 Google FCM 令牌可以包含非十六进制字符。因此,使用 PHP 的pack()函数打包 Google FCM 令牌将导致illegal hex digit错误。

回答by Ankit Makadiya

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

使用strtr(rtrim(base64_encode(pack('H*', sprintf('%u', $algo($data)))), '='), '+/', '-_')代替使用pack('H*', $value)

回答by Yogesh lele

In this case, $alert_deviceis an array.

在这种情况下,$alert_device是一个数组。

For packing it needs a value.

对于包装,它需要一个值。

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

使用 pack('H*', str_replace(' ', '', $alert_device[0]))来代替。

回答by Nageswara Rao

You must change

你必须改变

pack('H*', $someString)

To

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

回答by chings228

Try to save your file in utf-8 encoding.

尝试以 utf-8 编码保存您的文件。