[英]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 = "\\\錢";
提取wchar_t string s1 = "\\\錢";
(閱讀時會得到什么)。 很簡單,只需跳過前兩個字符並將其讀取為十六進制:
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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.