异或“Hello World!”切断字符串 [英] XORing "Hello World!" cuts off string
问题描述
#include< stdio.h>
#include< string.h>
int main()
{
char greeting [] =\\\
Hello World!\\\
;
int a;
for(int i = 0; i greeting [i] ^ = 111;
for(int i = 0; i greeting [i] ^ = 111;
printf(%s\\\
,greeting);
scanf(%d,& a);
}
输出:
$ b 地狱
发现对应于XOR键的数字(在这种情况下,ASCIIw)的字母?在数学逻辑中, N ^ N = 0
和 0 ^ N = N
,不是吗?
因为'o'
是ASCII码111,与111产生0, NUL
,并终止您的字符串。一旦发生这种情况(即使在第一个循环中,由于你每次通过循环来评估它), strlen
报告字符串要短得多,循环停止。 / p>
在进行异或之前保存字符串长度将会保存这些。
#include <stdio.h>
#include <string.h>
int main()
{
char greeting[]="\nHello World!\n";
int a;
for(int i=0; i<strlen(greeting); i++)
greeting[i]^=111;
for(int i=0; i<strlen(greeting); i++)
greeting[i]^=111;
printf("%s\n",greeting);
scanf("%d",&a);
}
Output:
Hell
Why does it cut everything after spotting a letter corresponding to the XOR key's number (in this case, ASCII 'w')? In mathematical logic, N^N=0
and 0^N=N
, doesn't it?
Because 'o'
is ASCII code 111, and XORing 111 with 111 yields 0, NUL
, and terminates your string. Once this happens (even in the first loop, since you're evaluating it each time through the loop), strlen
reports the string is much shorter, and the loops stop.
Saving the string length before going through the XORs will save you from this.
这篇关于异或“Hello World!”切断字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!