如何在C++中将像"u94b1"这样的字符串转换为一个真实字符?
How can I convert string like "u94b1" to one real character in C++?
我们知道在字符串文字中,"\u94b1"将被转换为一个字符,在这种情况下是一个中文单词"钱"。但是,如果它实际上是字符串中的 6 个字符,比如说"\"、"u"、"9"、"4"、"b"、"1",我如何手动将其转换为字符?
例如:
string s1;
string s2 = "u94b1";
cin >> s1; //here I input u94b1
cout << s1 << endl; //here output u94b1
cout << s2 << endl; //and here output 钱
我想转换s1
,以便cout << s1 << endl;
也会输出钱
.
有什么建议吗?
实际上转换有点复杂。
string s2 = "u94b1";
实际上相当于:
char cs2 = { 0xe9, 0x92, 0xb1, 0}; string s2 = cs2;
这意味着您正在初始化构成金钱的 UTF8 表示形式的 3 个字符 - 您只需检查s2.c_str()
以确保这一点。
因此,要处理 6 个原始字符 ''、'u'、'9'、'
4'、'b'、'1',您必须首先从string s1 = "\u94b1";
中提取wchar_t(阅读时得到的内容)。这很简单,只需跳过前两个字符并将其读为十六进制:
unsigned int ui;
std::istringstream is(s1.c_str() + 2);
is >> hex >> ui;
ui
现在0x94b1
.
现在,如果您有一个兼容 C++11 的系统,您可以使用 std::convert_utf8
进行转换:
wchar_t wc = ui;
std::codecvt_utf8<wchar_t> conv;
const wchar_t *wnext;
char *next;
char cbuf[4] = {0}; // initialize the buffer to 0 to have a terminating null
std::mbstate_t state;
conv.out(state, &wc, &wc + 1, wnext, cbuf, cbuf+4, next);
cbuf
现在包含 utf8 中代表钱的 3 个字符和一个终止 null,您可以最终执行以下操作:
string s3 = cbuf;
cout << s3 << endl;
为此,
您可以编写代码来检查字符串是否包含反斜杠、字母 u 和四个十六进制数字,并将其转换为 Unicode 代码点。然后,您的 std::string 实现可能采用 UTF-8,因此您将该代码点转换为 1、2 或 3 个 UTF-8 字节。
对于加分,请弄清楚如何在基本平面之外输入码位。
使用 utfcpp(仅限标头),您可以执行以下操作:
#include </usr/include/utf8.h>
#include <cstdint>
#include <iostream>
std::string replace_utf8_escape_sequences(const std::string& str) {
std::string result;
std::string::size_type first = 0;
std::string::size_type last = 0;
while(true) {
// Find an escape position
last = str.find("\u", last);
if(last == std::string::npos) {
result.append(str.begin() + first, str.end());
break;
}
// Extract a 4 digit hexadecimal
const char* hex = str.data() + last + 2;
char* hex_end;
std::uint_fast32_t code = std::strtoul(hex, &hex_end, 16);
std::string::size_type hex_size = hex_end - hex;
// Append the leading and converted string
if(hex_size != 4) last = last + 2 + hex_size;
else {
result.append(str.begin() + first, str.begin() + last);
try {
utf8::utf16to8(&code, &code + 1, std::back_inserter(result));
}
catch(const utf8::exception&) {
// Error Handling
result.clear();
break;
}
first = last = last + 2 + 4;
}
}
return result;
}
int main()
{
std::string source = "What is the meaning of '\u94b1' '\u94b1' '\u94b1' '\u94b1' ?";
std::string target = replace_utf8_escape_sequences(source);
std::cout << "Conversion from "" << source << "" to "" << target << ""n";
}
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 转换特殊字符(UTF-8)
- 将转换字符键入 int 以用作向量C++的索引
- 将字符随机转换为大写的函数
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 转换字符时Arduino DUE 和 Arduino UNO 之间的区别
- 如何在C++中正确重载运算符转换字符*?
- 如何转换数据结构以在 c++ 中转换字符*
- 不兼容的转换字符
- 将转换字符数组[16]键入为int 128位
- 在c++中转换字符编码
- 使用递归转换字符
- 如何在Objective-C/C/C++中转换字符和字节的位置
- C++到Pep/8汇编语言的转换——字符数组
- 转换字符>十六进制