简体   繁体   English

将 int arr 转换为 C 中的字符串时随机出现奇怪的字符(使用有效的 ASCII)

[英]Weird characters randomly appearing when converting int arr to string in C (with valid ASCII)

This is a simple Caesar cipher implementation in C.这是 C 中的简单凯撒密码实现。

I take a pin, make a key from it, take a message, shift the ASCII value w.r.t the key, and output the hex value of each character in hex.我拿了一个针,从中制作一个密钥,接收一条消息,将 ASCII 值 w.r.t 转换为密钥,并将 output 转换为十六进制中每个字符的十六进制值。

Upon using (as it appeared to me) any key , for the some (not all) messages results in the printing of an extra character upon decoding.在使用(在我看来)任何 key时,对于某些(不是全部)消息会导致在解码时打印额外的字符。

  • Example Cases:示例案例:

Weird Char appearing:奇怪的字符出现:

Message: " hexa ! "消息:“ hexa !

Pin: 454545 & Key: 23引脚: 454545 & 密钥: 23

Ciphertext: (in hexa) " 51 4e 61 4a 9 a fffffff3 4e -6f " (-6f is simply used to terminate input)密文:(十六进制)“ 51 4e 61 4a 9 a fffffff3 4e -6f ”(-6f 仅用于终止输入)

Text given when decoding:解码时给出的文本:

hexa !
e

Other keys generate other weird chars, like ' + ', for example.其他键会生成其他奇怪的字符,例如“ + ”。 The weird char always appears on the next line.奇怪的字符总是出现在下一行。


The entire code is ~ 100 lines, so I wont paste it here, but it's available on GitHub .整个代码大约 100 行,所以我不会在这里粘贴,但它可以在 GitHub 上找到 Don't use the windows.exe in this repo, it is of the older version, I'm trying to fix this issue before releasing this version.不要在这个 repo 中使用 windows.exe,它是旧版本,我试图在发布这个版本之前解决这个问题。

The code where the issue is likely appearing is the encrypt() and decrypt() functions:可能出现问题的代码是 encrypt() 和 decrypt() 函数:

void encrypt() {
     char msg[3001], ch;
     int i,key, en[3001], count = 0;

     printf("\n");
     key = pin();
     getchar();
     printf("\nType Message - \n\n");
     fgets(msg,sizeof(msg),stdin);

     for (i=0; msg[i] != '\0'; i++) {
         ch=msg[i];
         int d = (ch - key);
         en[i] = d;
         count++;
     }

     printf("\nEncrypted message -\n\n");

     for (i=0; i <= count; i++) 
         printf("%x ", en[i]);

     printf("-6f");
}

void decrypt() {
     char msg[3001], ch;
     int i,key, en[3001],d;

     printf("\n");
     key = pin();
     printf("\nEnter encrypted message -\n\n");
     getchar();

     for (i=0; i <= 3001; i++) {
         scanf("%x",&d);

         if (d == -111) {
            msg[i] = '\0';
            break;
         } else {
            ch = d + key;
            msg[i] = ch;
         }
     }

     printf("\nDecrypted message -\n\n");
     puts(msg);
}

In your second for loop:在你的第二个for循环中:

for(i = 0; i <= count; i++)

You are running off the end of the array (array indices start at zero, not one).您正在运行数组的末尾(数组索引从零开始,而不是一)。

Change it to:将其更改为:

for(i = 0; i < count; i++)

Always put your variable declarations each on their own line.总是把你的变量声明放在自己的行上。

int a, b, c = 0;        // only one variable is initialized.
char *src = 0, c = 0;   // c is of type char, not char*

fgets includes the trailing newline. fgets包括尾随换行符。 If you don't want it, you have to strip it off.如果你不想要它,你必须把它剥掉。

int len = strlen(msg);
if(len > 0 && msg[len - 1] == '\n')
   msg[len - 1] = '\0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM