0
I’m developing a communication socket between a tracker and the server. The socket is working perfectly.
My problem is with communication when it comes to checking the checksum.
The device sends a message ranging from 0 to 200 bytes to the server converting to hexadecimal of the: 2929b1000725cadc610ce80d
which I turn into an array that looks like this:
{["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
, the 10th position of the array is the checksum e8
which is calculated with a XOR sum, thus:
29 Xor 29 Xor b1 Xor 00 Xor 07 Xor 25 Xor ca Xor dc Xor 61 Xor 0c = e8
.
Soon I developed the following excerpt to accomplish this sum:
// transformando a 1ª posição em bit
$checksum = pack('H*',$data[0]);
// correndo o array
for ($i = 1; $i < count($data)-2; $i++) {
// Fazendo a soma XOR
$checksum ^= pack('H*',$data[$i]);
}
return $checksum;
where $data
is the array I showed above.
The result that this giving is d7
;
*Obs. the values I have shown are print_r
that I gave in the variables.
What is wrong?
I put the script to run and noticed that it is giving irregularity in the result.
01-12-17 03:33:38pm Connection from 186.227.156.12:18747
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}
01-12-17 03:33:38pm CheckSUM: cd
01-12-17 03:33:48pm Connection from 186.227.156.12:18747
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}
01-12-17 03:33:48pm CheckSUM: 6b
01-12-17 03:33:58pm Connection from 186.227.156.12:18747
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}
01-12-17 03:33:58pm CheckSUM: 6b
01-12-17 03:34:08pm Connection from 186.227.156.12:18747
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}
01-12-17 03:34:08pm CheckSUM: 6b
01-12-17 03:34:17pm Connection from 186.227.156.12:18747
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}
01-12-17 03:34:17pm CheckSUM: 6b
01-12-17 03:34:17pm Connection to 186.227.156.12:18747 closed
Handle ...
01-12-17 03:34:20pm Connection from 186.227.156.12:23463
Interact ...
01-12-17 03:34:22pm Connection from 186.227.156.12:23463
RECV -- {["29","29","b1","0","7","25","ca","dc","61","c","e8","d"]}
IMEI -- {37749297}