PHP 16进制异或效验

2022/4/22 17:12:50

本文主要是介绍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

 



这篇关于PHP 16进制异或效验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程