将字符串转换为unsigned char

时间:2013-11-28 23:10:43

标签: c++ aes unsigned-integer unsigned-char

我花了一段时间试图让一个字符串变成一个无符号字符数组,但没有运气。 我正在进行AES实现,除了输入读取部分外,我已经完成了所有设置。 这是我有ATM的代码。

string text = "E3 FD 51 12" ;
    int size = text.length();

    unsigned char* charText = new unsigned char[size/2];
    int i = 0;
    std::istringstream text_2(text);

    unsigned int c;
    unsigned char ch;
    while (text_2 >> hex >> c)
    {
        ch = c;
        cout <<ch;
    }

我希望我的字符串在charText数组中。字符串完全读入unsigned int,但是当我尝试将它放在数组或unsigned char(ch)中时,它给了我gibbrish。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

只需将char输出为int。

while (text_2 >> hex >> c)
{
    ch = c;
    cout << hex << ( int )ch;
}