Reputation: 133
Whenever I input string such as Dizzy with key 10 for example the output is partially wrong. I am having something like this ===>ns���
while I should have nsjji
.
Serial.print("KEY: ");
Serial.println(k);
if ((choice[0]=='e') || (choice[0]=='E')){
int i;
char ch;
for (i=0; str[i] != '\0'; i++){
ch=str[i];
if( ch >= 'a' && ch <= 'z'){
ch=ch+k;
if (ch >'z'){
ch=ch-'z'+'a'-1;
}
str[i]=ch;
}
else if(ch >='A' && ch <= 'Z'){
ch=ch+k;
if (ch > 'Z'){
ch=ch-'Z'+'A'-1;
}
str[i]=ch;
}
}
Serial.print("encrypt: ");
Serial.println(str);
Upvotes: 3
Views: 177
Reputation: 1946
The problem is that z has character value 122. Then you add 10 and you get 132. A char in C is -128 to 127 (signed), you probably want it to be 0-255 and then it has to be unsigned.
So when you get above the maximum of 127 you get the problems..
Change
char ch;
To this instead:
unsigned char ch;
Upvotes: 1