here)?
问题:
回答1:
Note this loops over UTF-16 code units, not actual code points.
回答2:
I assume you're doing code-generation (of JavaScript, maybe?)
QString is like a collection of QChar. Loop through the contents, and on each QChar call the unicode method to get the ushort (16-bit integer) value.
Then format each character like "\\u%04X", i.e. \u followed by the 4-digit hex value.
NB. You may need to swap the two bytes (the two hex characters) to get the right result depending on the platform you're running on.
回答3:
wchar_t *input; wstring output; for (int i=0; i<str_len; i++) { wchar_t code[7]; swprintf(code, 7, L"\\u%0.4X",input[i]); output += code; } 回答4:
I have solved the problem with this code:
EDITED TO A BETTER VERSION: (I just do not want to convert Latin symbols to Unicode, because it will consume additional space without and advantage for my problem (want to remind that I want to generate Unicode RTF)).
Thanks to @Daniel Earwicker for the algorithm and of course +1.
BTW you need to specify UTF-8 for text editor encoding.
回答5:
Hope it helps.
回答6:
My solution:
std::wstring output; QString result; QTextCodec::setCodecForLocale ( QTextCodec::codecForName ( "UTF-8" ) ); for( uint i = 0; wcslen( input ) > i; ++i ) { if( isascii( input[ i ] ) ) { output.reserve( output.size() + 1 ); output += input[ i ]; } else { wchar_t code[ 7 ]; swprintf( code, 7, L"\\u%0.4X", input[ i ] ); output.reserve( output.size() + 7 ); // "\u"(2) + 5(uint max digits capacity) output += code; } } result.reserve( output.size() ); result.append( QString::fromStdWString( output ) ); Works with Russian correct. Transforms
hello привет into
hello \\u043F\\u0440\\u0438\\u0432\\u0435\\u0442