首页 > 解决方案 > 试图移动字符串中的每一位

问题描述

尝试一个编码程序,它将转换字符串中每个字符中的 ascii 代码并打印出新字符,以便稍后我可以向左移动并解码消息。

例子

"#" = 35 或 100011

100011 左移一次 = 1000110 或 70

然后我想打印“F”。

到目前为止,这就是我所拥有的代码。我不明白输出。不知道是不是因为没有超过 127 的 ascii 字符的代码。

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    int i;

    string str ("Hello World");
    string encode, decode;


    for ( i=0; i<str.length(); ++i)
    {
        cout << str[i];
    }

    cout << endl << endl;

    for ( i=0; i<str.length(); ++i)
    {
        cout << (int) str[i] << " ";

    }

    cout << endl << endl;

    for ( i=0; i<str.length(); ++i)
    {
        encode[i] = (str[i] << 1) ;

        cout << encode[i]  << " ";
    }

    cout << endl << endl;

    return 0;
}

输出:

Hello World

72 101 108 108 111 32 87 111 114 108 100 

\220 \312 \330 \330 \336 @ \256 \336 \344 \330 \310 

标签: c++bit-shift

解决方案



推荐阅读