字符串中每个字符的异或总和
XOR sum of everysingle char in string
我从 Receive[300] 中得到以 delim1 - & 开头并以 delim2 - * 结尾的字符串。现在我需要对中间的所有字符进行异或运算。我认为我需要将每个字符转换为 bin,然后将它们相加。我一直在 "string.h" 中寻找函数,但里面什么也没有。我该如何解决这个问题?
使用 for
循环。
char *ptr = strtok(Received, delim1);
char *star = strtok(ptr, delim2);
char result = ptr[0]; // Start with the first character in the token
if (ptr[0]) { // don't loop if the string is empty
for (char *p = ptr+1; *p; p++) { // Loop over the remaining characters
result ^= *p; // XOR them into the result
}
}
我从 Receive[300] 中得到以 delim1 - & 开头并以 delim2 - * 结尾的字符串。现在我需要对中间的所有字符进行异或运算。我认为我需要将每个字符转换为 bin,然后将它们相加。我一直在 "string.h" 中寻找函数,但里面什么也没有。我该如何解决这个问题?
使用 for
循环。
char *ptr = strtok(Received, delim1);
char *star = strtok(ptr, delim2);
char result = ptr[0]; // Start with the first character in the token
if (ptr[0]) { // don't loop if the string is empty
for (char *p = ptr+1; *p; p++) { // Loop over the remaining characters
result ^= *p; // XOR them into the result
}
}