Fixed integer overflow issue in shifting
This commit is contained in:
parent
8e0d2faefb
commit
ca79610cfb
5
brute.cc
5
brute.cc
@ -10,10 +10,11 @@ void shift(char* text, int key)
|
||||
std::cout << ' ';
|
||||
continue;
|
||||
}
|
||||
char c = std::tolower(text[i])-key;
|
||||
// has to be int, because the result can sometimes be > 127
|
||||
int c = std::tolower(text[i])-key;
|
||||
if(c < 'a') c+=26;
|
||||
if(c > 'z') c-=26;
|
||||
std::cout << c;
|
||||
std::cout << (char)c;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
5
shift.cc
5
shift.cc
@ -20,10 +20,11 @@ int main(int argc, char** argv)
|
||||
std::cout << ' ';
|
||||
continue;
|
||||
}
|
||||
char c = std::tolower(text[i])+key;
|
||||
// has to be int, because the result can sometimes be > 127
|
||||
int c = std::tolower(text[i])+key;
|
||||
if(c < 'a') c+=26;
|
||||
if(c > 'z') c-=26;
|
||||
std::cout << c;
|
||||
std::cout << (char)c;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
|
@ -25,10 +25,11 @@ int main(int argc, char** argv)
|
||||
std::cout << ' ';
|
||||
continue;
|
||||
}
|
||||
char c = std::tolower(text[pi]) + keyvalue(key[ki]);
|
||||
// has to be int, because the result can sometimes be > 127
|
||||
int c = std::tolower(text[pi]) + keyvalue(key[ki]);
|
||||
if(c < 'a') c+=26;
|
||||
if(c > 'z') c-=26;
|
||||
std::cout << c;
|
||||
std::cout << (char)c;
|
||||
if(key[++ki] == 0) ki = 0;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
Loading…
Reference in New Issue
Block a user