Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助编码C++;通过增加ASCII值生成字符串 我到处查看了一个C++代码,它从用户那里获取消息,并通过增加每个字符的ASCII值来编码它(显然不是很安全,但足够简单)。我已经成功地编写了一个程序,该程序返回的字符值稍高一些,但无法解决如何使用包含空格的完整消息。我计划做一个解码器,之后做相反的事情。任何帮助都将不胜感激。提前谢谢 单值C++程序-< /P> #include <iostream> using namespace std; int main(){ char ascii; cout << "Enter a character: "; cin >> ascii; cout << "Its ascii value is: " << (int) ascii << endl; return 0; }_C++_String_Encryption_Encoding_Ascii - Fatal编程技术网

需要帮助编码C++;通过增加ASCII值生成字符串 我到处查看了一个C++代码,它从用户那里获取消息,并通过增加每个字符的ASCII值来编码它(显然不是很安全,但足够简单)。我已经成功地编写了一个程序,该程序返回的字符值稍高一些,但无法解决如何使用包含空格的完整消息。我计划做一个解码器,之后做相反的事情。任何帮助都将不胜感激。提前谢谢 单值C++程序-< /P> #include <iostream> using namespace std; int main(){ char ascii; cout << "Enter a character: "; cin >> ascii; cout << "Its ascii value is: " << (int) ascii << endl; return 0; }

需要帮助编码C++;通过增加ASCII值生成字符串 我到处查看了一个C++代码,它从用户那里获取消息,并通过增加每个字符的ASCII值来编码它(显然不是很安全,但足够简单)。我已经成功地编写了一个程序,该程序返回的字符值稍高一些,但无法解决如何使用包含空格的完整消息。我计划做一个解码器,之后做相反的事情。任何帮助都将不胜感激。提前谢谢 单值C++程序-< /P> #include <iostream> using namespace std; int main(){ char ascii; cout << "Enter a character: "; cin >> ascii; cout << "Its ascii value is: " << (int) ascii << endl; return 0; },c++,string,encryption,encoding,ascii,C++,String,Encryption,Encoding,Ascii,这真的很简单。首先,将输入作为字符串。然后,遍历每个字符,按所需添加内容进行添加。为确保该值保持有效且易于反转,可将该值修改为字符的最大值,即255 int main () { std::string input; // to store the text std::getline(std::cin, input); // get the text const int _add = 12; // value added const int max_size =

这真的很简单。首先,将输入作为字符串。然后,遍历每个字符,按所需添加内容进行添加。为确保该值保持有效且易于反转,可将该值修改为字符的最大值,即255

int main () {
    std::string input; // to store the text
    std::getline(std::cin, input); // get the text

    const int _add = 12; // value added
    const int max_size = 255; // max value of a char
    for (int i = 0; i < input.size(); ++i)
        input[i] = (input[i] + _add) % max_size;
    /* now the input is properly modified */
}
int main(){
std::string input;//用于存储文本
std::getline(std::cin,输入);//获取文本
const int _add=12;//增加值
const int max_size=255;//字符的最大值
对于(int i=0;i
注意:
\u add
是一个int,用于防止溢出错误。

std::string originalString=“”;
std::string originalString = "";
std::string newString = "";
int incrementValue = 1;
std::cout << "Input a string to encode: ";
std::cin >> originalString;
for(int i = 0; i < originalString.length(); i++) {
   newString += (originalString.at(i) + incrementValue);
}
std::cout >> "New string is " + newString
std::string newString=“”; int incrementValue=1; std::cout>原始字符串; 对于(int i=0;i>“新字符串是”+新字符串
只需更改
incrementValue
即可更改其编码方式。 “Hello”=“Ifmmp”如果incrementValue=1

要反转它,只需将其更改为减去
递增值
,而不是为
循环添加相同类型的
。很简单,我认为这可以在一行中完成,如果您想保持在80列以下,可以在两行中完成。其中,
value
是要加密的字符串,
offset
是偏移值:

auto caesar = [offset](CharT c){return c + offset;};
std::transform(value.begin(), value.end(), value.begin(), caesar);
为了获得额外积分,您可以通过在字符类型上模板化使其与任何类型的字符串一起工作:

template <typename CharT>
std::basic_string<CharT> caesarEncode(std::basic_string<CharT> value, CharT offset){
    auto caesar = [offset](CharT c){return c + offset;};
    std::transform(value.begin(), value.end(), value.begin(), caesar);
    return value;
}
对其实际编码字符串的操作如下:

char offset = 12;
auto encoded = caesarEncode(value, offset);

wchar_t woffset = 12;
auto wencoded = caesarEncode(wvalue, woffset);

您可以在实践中看到一个示例。

在Visual Studio中,您可以强制所有字符串变宽。在这种情况下,input.size()不会返回
char
的长度。虽然通常不需要它,但它不会产生错误的结果。我觉得我不配被否决:(老兄,CPluPlus一直是我的参考!它在
size()中写道)
将以字节为单位返回长度。cppreference说明了您所说的内容。我被复制了。从技术上讲,
std::string::size
是以字节和代码点为单位的,因为它们是相同的。但是cplusplus甚至没有提到wstring或basic_string。每个人都避免使用该站点。哦,这是:另请看:您的程序将通过一个例外当if
originalString.at(i)+incrementValue
大于255时,你应该将其修改为255:)1)*throw,而不是通过2)如果
char
的值超过255,它将被环绕,尽管技术上未定义的行为知道它是未定义的行为;我不知道它通常是包着的。不过,异常仍然是一个问题,是吗?不抛出您正在寻找的异常
// narrow (CharT = char)
std::string value;
std::getline(std::cin, value);

// wide (CharT = wchar_t)
std::wstring wvalue;
std::getline(std::wcin, wvalue);
char offset = 12;
auto encoded = caesarEncode(value, offset);

wchar_t woffset = 12;
auto wencoded = caesarEncode(wvalue, woffset);