PHP 16进制异或效验

公司需要烦恼呀,下面演示 8686808905171750601011 异或效验,判断最终程序是否效验正确地址:http://www.metools.info/code/c48.html

注:简单来说就是,有abc三个数,a异或b异或c = d

首先把 8686808905171750601011 拆分成 86 86 80 89 05 17 17 50 60 10 11

$str = '8686808905171750601011';
$hex = [];
for($i = 0;$i < strlen($str); $i++ ) {
    
    if(($i % 2) == 0) {
        $hex[] = strtoupper(substr($str,$i,2));
    }
}

然后编写两个函数

/**
 * # +========================================================================
 * # | - @name        hex数据BBC异或校验(两两比较)
 * # | - @author     cq <just_leaf@foxmail.com> 
 * # | - @copyright zmtek 2020-11-24
 * # +------------------------------------------------------------------------
 * # | - 返回结果
 * # +========================================================================
 */
function hexOr($byte1, $byte2)
{
    
    $result = '';
    $byte1     = str_pad(base_convert($byte1, 16, 2), '8', '0', STR_PAD_LEFT);
    $byte2     = str_pad(base_convert($byte2, 16, 2), '8', '0', STR_PAD_LEFT);
    $len1     = strlen($byte1);
    for ($i = 0; $i < $len1 ; $i++) {
        
        $result .= $byte1[$i] == $byte2[$i] ? '0' : '1';
    }
    
    return strtoupper(base_convert($result, 2, 16));
}

/**
 * # +========================================================================
 * # | - @name        hex数据BBC异或校验(多个hex数据进行校验)
 * # | - @author     cq <just_leaf@foxmail.com> 
 * # | - @copyright zmtek 2020-11-24
 * # +------------------------------------------------------------------------
 * # | - 返回结果
 * # +========================================================================
 */
function hexOrArr($data)
{
    $result = $data[0];
    for ($i = 0; $i < count($data) - 1; $i++) {
        $result = hexOr($result, $data[$i + 1]);
    }
    return $result;
}

开始调用

echo hexOrArr($hex);

得到的结果是:3D

 

posted @ 2022-04-22 16:51  正义的棒棒糖  阅读(266)  评论(0编辑  收藏  举报