gpt4 book ai didi

c - 对字符数组执行 'xor' sumcheck

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:08 24 4
gpt4 key购买 nike

我已经创建了字符数组(GPS 模块的第一行),我想执行 NMEA sumcheck,它是“$”和“*”之间每个字符的异或。

代码如下:

#include <stdio.h>

int main(void) {
int i;
int xor = 0;
int loop;

// $GPGLL,,,,,,V,N*64

char Received[18]= {'$','G','P','G','L','L',',',',',',',',',',',',','V',',','N','*','6','4'};

// display array if needed
//for(loop = 0; loop < 18; loop++)
// printf("%c ", Received[loop]);

for(int i = 2; i<=14; i++)
xor ^= Received[i];
printf("%d ", xor);
return 0;
}

这是演示 DEMO

你知道为什么 xor sumcheck 不同于 "64"吗?

最佳答案

我可以看到两个问题:

  1. for 循环应该从1开始
  2. 十六进制的校验和(0x64 是 100)

请看下面的代码:

for(int i = 1; i<=14; i++) {
xor ^= Received[i];
}
printf("%#02x ", xor);

编辑:要验证校验和,您可以使用 strtol 将字符串转换为数字:

int number = (int)strtol(Received+16, NULL, 16);
if (number == xor) { printf("ok "); } else { printf("err "); }

关于c - 对字符数组执行 'xor' sumcheck,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52512484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com