从 ASCII 转换回字符
Converting from ASCII back to char
我有一个函数已经将字符串转换为 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"。
相关文章:
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 字符转换功能 std::isupper() & std::islower() C++17
- 将字符 * 转换为字符串 *
- 在编译时将常量字符* 转换为常量 char_type*
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 无法使用字符串流将字符转换为字符串C++
- 将无符号字符 C++ 转换为 C#
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法<string>从"常量字符 []"转换为<类名>
- 不能将字符转换为整数吗?
- 编译时将字符*转换为字节
- CP1251:从字符* 转换为 wchar_t* 时的编码失真
- 将单个字符转换为 std::string 前缀 \x01
- PPM 将字符转换为 int 授予负数
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类
- 将常量字符* 转换为字符时出错