gpt4 book ai didi

C. ASCII 十六进制值数组的校验和计算

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

我正在尝试计算 ASCII 十六进制值数组的校验和。

假设我有以下内容

char exArray[] = "3030422320303030373830434441453141542355";

这是 20 对十六进制值,每个代表一个 ASCII 字符(例如 0x41 = A)。如何拆分它们以计算校验和?

或者,如何将数组中的两个值合并为一个值?(例如“4”、“1”->“41”)

最佳答案

@pmg:

First step would be converting the string representation (in hex) to an integer.

For the 2nd part, try ('4' - '0') * 16 + ('1' - '0')

这最终成功了,喜欢它的简单性,

我的实现现在看起来有点像这样。

uint8_t t = 0, tem, tem2, sum;
uint32_t chksum = 0;

void checkSum(void)
{
while (t < 40)
{
asciiToDec(exArray[t]);
tem = global.DezAscii[0];
t++;
asciiToDec(exArray[t]);
tem2 = global.DezAscii[0];
t++;

sum = (tem) * 16 + (tem2);
chksum += sum;
}
}

void asciiToDec(uint8_t value)
{
if (value == 'A')
global.DezAscii[0] = 10;
else if (value == 'B')
global.DezAscii[0] = 11;
else if (value == 'C')
global.DezAscii[0] = 12;
else if (value == 'D')
global.DezAscii[0] = 13;
else if (value == 'E')
global.DezAscii[0] = 14;
else if (value == 'F')
global.DezAscii[0] = 15;
else
global.DezAscii[0] = value;
}

关于C. ASCII 十六进制值数组的校验和计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52511728/

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