gpt4 book ai didi

php - 将 HEX 转换为 ASCII,来自 GPS 跟踪器的数据

转载 作者:可可西里 更新时间:2023-11-01 02:49:46 24 4
gpt4 key购买 nike

我刚买了一个 GPS 追踪器,它可以向手机发送短信就好了。它还支持通过 GPRS 向服务器报告。

我已将设备设置为在端口 8123 上连接我自己的服务器,它是一个 FreeBSD 服务器,我已检查我是否在该端口上收到数据包。

我成功地设置了一个用 PHP 编写的监听器服务器,并且我可以从设备接收数据。但是我如何将部分 HEX 数据转换为有用的东西 (ASCII)?

示例数据字符串:

$$^@T^@E Y'^WÿU210104.000,A,5534.4079,N,01146.2510,E,0.00,,170411,,*10|1.0|72|0000á

Unfortunately i don't know how i can copy-paste the HEX parts

Now how do i get the ID part out? I have tried echo hexdec(mb_substr($data, 4, 7));

The data is following this protocol

From the document:

Command format of GPRS packets are as follows:From server to tracker:@@\r\n From tracker to server: $$\r\nNote: Do NOT input ‘’ when writing a command. All multi-byte data complies with the following sequence: High byte prior to low byte. The size of a GPRS packet (including data) is about 100 bytesItem        Specification@@          2 bytes. It means the header of packet from server to tracker.             It is in ASCII code (Hex code: 0x40)$$          2 bytes. It is the header of packet from tracker to server.            It is in ASCII code (Hex code: 0x24)L           2 bytes. It means the length of the whole packet including             the header and ending character and it is in hex codeID          7 bytes, ID must be digit and not over 14 digits, the unused byte             will be stuffed by ‘f’ or ‘0xff’. It is in the format of hex code.            For example, if ID is 13612345678, then it will be shown as             follows: 0x13, 0x61, 0x23, 0x45, 0x67, 0x8f, 0xff.            If all 7 bytes are 0xff, it is a broadcasting command. ID is in hex codecommand     2 bytes. The command code is in hex code. Please refer to the             command list below.data        Min 0 byte and max 100 bytes. See Annex 1 for description of ‘data’.checksum    2 bytes. It indicates CRC-CCITT (default is 0xffff) checksum of            all data (not including CRC itself and the ending character).            It is in hex code.             For example: 24 24 00 11 13 61 23 45 67 8f ff 50 00 05 d8 0d 0a            0x05d8 = CRC-CCITT (24 24 00 11 13 61 23 45 67 8f ff 50 00)\r\n        2 bytes. It is the ending character and in hex code             (0x0d,0x0a in hex code)

Update

With the answer from Anomie, i was able to piece this together

$arr = unpack('H4length/H14id/H4cmd/H4crc/H4end', mb_substr($data, 2, 11) . mb_substr($data, -4));

var_dump($arr);

这会输出类似的东西

array(5) {  ["length"]=>  string(4) "0054"  ["id"]=>  string(14) "004512345678ff"  ["cmd"]=>  string(4) "9955"  ["crc"]=>  string(4) "c97e"  ["end"]=>  string(4) "0d0a"}

最佳答案

听起来您需要将二进制数据转换为整数或字符串。最直接的方法是使用 unpack .

例如,要提取您已知的长度,您可以使用

$length_bin = substr($string, 2, 2);

要将其转换为整数,您可以使用类似的东西

$length = unpack('v', $length_bin); $length = $length[1];

'v' 代码将用于长度和校验和;如果您将数字存储为 4 个字节,请使用 'V',对于 ID,您可以使用 'H*' 将其作为十六进制数字字符串获取。其他代码列于the documentation .


一种不太直接的方法是在使用 C* 解包以获取所有字节值的数组后,手动进行位操作。例如,

$bytes = unpack('C*', $length_bin);
$length = ($bytes[0] << 8) | $bytes[1];

关于php - 将 HEX 转换为 ASCII,来自 GPS 跟踪器的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5633467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com