我正在为那些看过我之前帖子的人开发相同的加密程序。现在程序将接受字母,它显示了从 ascii 到字母的转换,但是有一个问题。在我对字母的值添加任何计算之前,它会打印得很好。例如,如果我输入 f,我得到 102。但是,当我尝试计算时,例如将 ascii 值乘以选定的素数,该值本身变得很大! 这是代码:
#include<stdio.h>
int main(){
int x,y,z;
int encrypt_number[] = {7507,55529,77933,142867,392263};
printf("\tCaleb's encryption machine\n");
printf("Please enter a letter to be encrypted:\n");
scanf("%[a-z]s",&x);
printf("Enter your encryption key number between one and five\n");
scanf("%d",&y);
if (y == 1)
y = encrypt_number[0];
else if ( y == 2 )
y = encrypt_number[1];
else if ( y == 3 )
y = encrypt_number[2];
else if ( y == 4 )
y = encrypt_number[3];
else if ( y == 5 )
y = encrypt_number[4];
else
printf("not a valid option\n");
z = x*y;
printf("Your encrypted value is: %d\n",z);
printf("\tdiagnosis of encryption:\n");
printf("Letter was: %c\n",x);
printf("value in ascii was: %d\n");
}
在上面的例子中,f 变成了 264241254!希望这只是一个简单的错误?
最佳答案
在打印
行
printf("value in ascii was: %d\n");
您只提供格式说明符 %d
但不传递您想要 print.
的参数。
关于c - 加密程序ascii问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23051848/