日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

BCC异或校验 Linux C

發布時間:2023/12/10 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BCC异或校验 Linux C 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、BCC異或校驗

從輸入的HEX第一個字節開始,按字節依次循環計算異或值直到HEX結尾字節,得到的最終一個字節值。

2、C程序

/******************************************************************************* _____ ___ ____ ___ _____ _ _ _ | ____|_ _| _ \|_ _| |_ _|__(_)_ __ __ _| | | |_ _ __ _ | _| | || |_) || | | |/ __| | '_ \ / _` | |_| | | | |/ _` | | |___ | || _ < | | | |\__ \ | | | | (_| | _ | |_| | (_| | |_____|___|_| \_\___| |_||___/_|_| |_|\__, |_| |_|\__,_|\__,_|* File Name : main.c* Description : This file provides code for bcc caculation in linuxc.* Author : jackwang by jiawang16@foxmail.com* Date : 2019-03-09 *******************************************************************************/ /*! -------------------------------------------------------------------------- */ /*! Include headers */ #include <stdio.h> #include <string.h> #include <stdbool.h>/*! -------------------------------------------------------------------------- */ /*! Private function declarations */ static unsigned char Char2Int(char chr,bool *isOK);/*! convert char to int type*/ static unsigned char HexStr2Int(char *str, bool *isOK);/*!convert hexstr to int*//*! -------------------------------------------------------------------------- */ /*! main function defination */ int main(int argc, char* argv[]) {int ret = 0;int numByte = argc; char bccVal = 0x00;char inPutbuff[10];bool isOK;int Nibb;if(argc == 1){printf("[note] no params to caculate, please input hex string, splite by space!\r\n");}else{printf("[note] input %d byte: ",numByte-1);for(int i = 1; i < numByte; i++){printf("%s ",argv[i]);}printf("\r\n");for(int i = 1; i < numByte; i++){memcpy(inPutbuff,argv[i],2);Nibb = HexStr2Int(inPutbuff,&isOK);if(isOK){bccVal ^= (char)Nibb;}}printf("\r\n");printf("[note] bcc value: %02X\r\n",bccVal);}return 0; }/*! -------------------------------------------------------------------------- */ /*! Private function definations */ /*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ static int Char2Int(char chr,bool *isOK) {int nibb1;if(chr >= '0' && chr <= '9'){ nibb1 = chr - '0'; *isOK = true;}else if(chr >= 'a' && chr <= 'f'){ nibb1 = chr - 'a' + 10; *isOK = true;}else if(chr >= 'A' && chr <= 'F'){ nibb1 = chr -'A' + 10; *isOK = true; }else{ printf("[error] invalid hex str input: %c \r\n",chr); *isOK = false; }return nibb1; } /*! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ static int HexStr2Int(char *str, bool *isOK) {int nibb1,nibb2;bool isOK1,isOK2;nibb1 = Char2Int(*str, &isOK1);nibb2 = Char2Int(*(str+1),&isOK2);if(isOK1 && isOK2){*isOK = true;return nibb1*16 + nibb2;}else{*isOK = false;return 0;} }

3、編譯

~$ gcc main.c -o getbcc

4、使用

~$ ./getbcc 01 02 03 04 11 ~$ [note] input 5 byte: 01 02 03 04 11 ~$ [note] bcc value: 15

?

總結

以上是生活随笔為你收集整理的BCC异或校验 Linux C的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。