从 ASCII 转换回字符

Converting from ASCII back to char

本文关键字:字符 转换 ASCII      更新时间:2023-10-16

我有一个函数已经将字符串转换为 ASCII 整数,但我如何做相反的事情?谢谢

你的问题不清楚。基于假设您的 ASCII 整数(在您的术语中)存储在vector<int>中为您提供解决方案

下面的函数会将其转换为字符串:

std::string
AsciiIntToString ( std::vector<int> const& ascii_ints ) 
{
    std:: string ret_val;
    std::vector<int>:: const_iterator it = ascii_ints. begin ();
    for ( ; it != ascii_ints. end (); ++it ) {
        if ( *it < 0 || *it > 255) throw std::exception ("Invalid ASCII code");
        ret_val += static_cast<char>(*it);
    }
    return ret_val;
}
下面是

一些使用 std::bitset 在数字和二进制格式的数字文本表示之间进行转换的示例(仅适用于可以用 7 位表示的字符集(例如 US-ASCII)):

char c = 'a';
// char to int.
int i = static_cast<int>(c);
// int to string (works for char to string also).
std::string bits = std::bitset<8>(i).to_string();
// string to (unsigned long) int.
unsigned long ul = std::bitset<8>(bits).to_ulong();
// int to char.
c = static_cast<char>(ul);

这是一个更简单的方法!

void convertToString()
{
    char redo;
    int letter;
    int length;
    do{
        cout<< "How long is your word n";
        cin >> length;
        cout << "Type in the letter values n";
        for (int x = 0; x < length; x++)
        {
            cin >> letter;
            cout << char (letter);
        }
        cout << "n To enter another word hit R" << endl;
        cin >> redo;
    } while (redo == 'R');    
}

新词"ASCII 'int'"的使用是对ASCII代码的不精确 - 但并非不清楚 - 引用。参考很清楚,因为所有的ASCII码都是整数,就像整数一样。

最初的海报能够将ASCII字符翻译成十进制,大概是使用了一个函数。

在MySQL中,这将是:选择ASCII('A') [从DUAL];,返回65。

要反转方向,请使用 char() 函数:选择字符(65) [从双];

也许这对你来说是一个很好的解决方法。

我建议使用非 GUI 客户端。

static转换为cast的最佳方法是

int it=5;
char W = static_cast<char>(*it);

你只需要把它存储在一个char变量中:

//Let's say your original char was 'A'...
int asciivalue = int('A');
// Now asciivalue = 65
//to convert it back:
char orig = asciivalue;
cout << orig << endl;

它将输出"A"。