我有几个问题:
- 如何从字符串中获取某个字符?
- 如何将 char 转换为 int?
- 如何将 int 转换为 char?
- 如何将字符附加到字符串?
我只是在制作一个简单的 key 密码……只是为了学习 cpp 而四处游玩。我确实懂 Java,所以如果你能与之相关,那就太好了!
这是我的代码,所以请告诉我如何改进...谢谢! :)
#include <string>
using namespace std;
string encrypt(string data, string pass) {
// Use a Keyed Cipher //
string encrypted;
int index = 0;
for (int x = 0; x < sizeof(data); x++) {
int tmp = static_cast<int>(data.substr(x));
int tmpPass = static_cast<int>(pass.substr(index));
tmp += tmpPass;
if (tmp > 126) {
tmp -= 95;
}
if (index > sizeof(pass)) {
index = 0;
}
encrypted += static_cast<char>(tmp);
}
return data;
}
最佳答案
How do you get a certain char from a string?
通过使用索引运算符。 string::operator[]
How do you get a char to a int?
int charToInteger = stringName[i] ;
How do you append a char to a string?
来自链接-
string& append ( size_t n, char c );
Appends a string formed by the repetition n times of character c.
关于c++ - 您如何从一个变量更改为另一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5525510/