php unpack()用H *或bin2hex()剥离前导零

时间:2015-01-27 08:51:22

标签: php unpack

我有一些前导零字节的二进制数据,但是当我解压缩这些数据并将其转储为十六进制字符串时,我似乎丢失了这些字节。我做错了什么?

更多细节......

我在php中集成GPS设备数据。

进入设备的数据是二进制格式,我正在使用

$arr = unpack("H*", $input);

我将数据作为

01aa08080000014b27163dfc00328e2c950d3edc0a00c2010b0f000e00080301010200f00103091f4542374318000e01c700000004014e00000000000000000000014b271645cc00328e29110d3edc4e00c301160f001000080301010200f001030920814236e818001001c700000008014e00000000000000000000014b2716518400328e24e10d3edc8700c301040e000800080301010200f00103091f8242371518000801c70000000d014e00000000000000000000014b2716a40400328e21510d3edd5c00c6013a0e000800080301010200f00103091f3d4236bc18000801c700000001014e00000000000000000000014b2717dd5600328e1e060d3edd1300cd01080d0000fa090401000200f000fa000309000e4232e718000001c700000000014e00000000000000000000014b2719b86000328e1e060d3edd1300d001080d000000050301000200f0000209001142328b00000000014b271b8fb400328e1e060d3edd1300d001080d000000050301000200f0000209001142327200000000014b271d69b000328e1e060d3edd1300d001080d000000050301000200f0000209001142325e0000080000a771

or 

2d08010000014b275faaa000328e1e060d3edd1300d001080d000000050301000200f000020900124231e400000100002144

这是一种错误的数据格式,因为前导零都被剥离了。 根据集成文档,数据将以

开头
Four zero bytes (0x00)

4B AVL Data Length in the format 0000xxxx

前两个数据的正确格式应为

00000000000001aa08080000014b27163dfc00328e2c950d3edc0a00c2010b0f000e00080301010200f00103091f4542374318000e01c700000004014e00000000000000000000014b271645cc00328e29110d3edc4e00c301160f001000080301010200f001030920814236e818001001c700000008014e00000000000000000000014b2716518400328e24e10d3edc8700c301040e000800080301010200f00103091f8242371518000801c70000000d014e00000000000000000000014b2716a40400328e21510d3edd5c00c6013a0e000800080301010200f00103091f3d4236bc18000801c700000001014e00000000000000000000014b2717dd5600328e1e060d3edd1300cd01080d0000fa090401000200f000fa000309000e4232e718000001c700000000014e00000000000000000000014b2719b86000328e1e060d3edd1300d001080d000000050301000200f0000209001142328b00000000014b271b8fb400328e1e060d3edd1300d001080d000000050301000200f0000209001142327200000000014b271d69b000328e1e060d3edd1300d001080d000000050301000200f0000209001142325e0000080000a771

and 

000000000000002d08010000014b275faaa000328e1e060d3edd1300d001080d000000050301000200f000020900124231e400000100002144

注意4B前导零和4B数据长度,零填充。

当我使用

$arr = bin2hex($value)

它给了我相同的结果。

但我确信数据存在于二进制文件中,我无法呈现前导零。

请帮忙。

提前致谢

1 个答案:

答案 0 :(得分:-1)

对代码的审查注意到我正在做二进制数据。

$input = trim($input);

造成了这个问题。

注释代码和unpack返回所需的确切格式