c语言实现异或校验

  异或校验是验证数据通信结果的一种常用手段,下面是一个异或校验的函数,输入需要校验的数据数组以及长度,返回异或校验的结果。

 

 

#include<stdio.h>

typedef unsigned          char uint8_t;
uint8_t XOR_check(uint8_t *Buf, uint8_t Len)
{
uint8_t i = 0;
uint8_t x = 0;

for(i=0; i<Len; i++)
{
x = x^(*(Buf+i));
}

return x;
}

int main()
{
    uint8_t tx[2] = {0x11,0x22};
    uint8_t s;
    s = XOR_check(tx,2);

    printf("result=%x\n",s);
    
}

  

 

posted @ 2021-12-01 09:55  夜望繁星  阅读(3760)  评论(0编辑  收藏  举报