01 A0 7C FF 02
01 xor A0 xor 7C xor FF xor 02 = 20
20
//hex数据BBC异或校验(两两比较)
function hexXor($hex1, $hex2){
$bin1 = base_convert($hex1, 16, 2);
$bin2 = base_convert($hex2, 16, 2);
$len1 = strlen($bin1);
$len2 = strlen($bin2);
$result = ‘‘;
//如果不相等判断补齐再异或
if($len1 != $len2){
if($len1 > $len2){
$temp = ‘‘;
for ($i=0; $i < $len1 - $len2; $i++) {
$temp.=‘0‘;
}
}else{
$temp = ‘‘;
for ($i=0; $i < $len2 - $len1; $i++) {
$temp.=‘0‘;
}
}
$bin2 = $temp.$bin2;//不足补0
}
for ($i=0; $i < $len1; $i++) {
$result .= $bin1[$i] == $bin2[$i] ? ‘0‘ : ‘1‘;
}
return base_convert($result, 2, 16);
}
//hex数据BBC异或校验(多个hex数据进行校验)
function hexXorArr($data){
$result = $data[0];
for ($i=0; $i < count($data)-1; $i++) {
$result = hexXor($result, $data[$i+1]);
}
return $result;
}
$string1 = ‘8a‘;
$string2 = ‘01‘;
$string3 = ‘12‘;
$string4 = ‘11‘;
echo hexXor(hexXor(hexXor($string1, $string2), $string3), $string4);
echo ‘<hr/>‘;
echo hexXorArr([$string1, $string2, $string3, $string4]);
原文:https://www.cnblogs.com/lina520/p/14156549.html