代码之家  ›  专栏  ›  技术社区  ›  Narek

如何将unicode字符串转换为其unicode转义?

  •  4
  • Narek  · 技术社区  · 15 年前

    说我有一条短信“你好°". (我将此代码保存在QString中,但如果您知道用其他方法将此文本存储在c++代码中,则不客气。)如何将此文本转换为Unicode转义符,如“\u1330\u1377\u1408\u1415 Hello\u1047\u1076\u1088\u1072\u1074\u1089\u1090\u1074\u1091\u1081”(请参阅 here )?

    6 回复  |  直到 15 年前
        1
  •  5
  •   Philipp    15 年前
    #include <cstdio>
    
    #include <QtCore/QString>
    #include <QtCore/QTextStream>
    
    int main() {
      QString str = QString::fromWCharArray(L"Բարև Hello Здравствуй");
      QString escaped;
      escaped.reserve(6 * str.size());
      for (QString::const_iterator it = str.begin(); it != str.end(); ++it) {
        QChar ch = *it;
        ushort code = ch.unicode();
        if (code < 0x80) {
          escaped += ch;
        } else {
          escaped += "\\u";
          escaped += QString::number(code, 16).rightJustified(4, '0');
        }
      }
      QTextStream stream(stdout);
      stream << escaped << '\n';
    }
    

    请注意,这是在UTF-16代码单元上循环的,而不是实际的代码点。

        2
  •  3
  •   Daniel Earwicker    15 年前

    QString 就像一堆 QChar . 循环浏览内容,并在每个 夸夸尔 打电话给 unicode 获取 ushort (16位整数)值。

    然后将每个字符格式化为 "\\u%04X" ,即。 \u

    注意。您可能需要交换两个字节(两个十六进制字符)以获得正确的结果,具体取决于您运行的平台。

        3
  •  2
  •   Petro    14 年前
    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
  •  1
  •   Narek    13 年前

    编辑成一个更好的版本:(我只是不想把拉丁符号转换成Unicode,因为它会占用额外的空间,对我的问题没有好处(想提醒一下,我想生成Unicode RTF))。

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
        QString str(QWidget::tr("Բարև (1-2+3/15,69_) Hello {} [2.63] Здравствуй"));
        QString strNew;
    
        QString isAcsii;
        QString tmp;
        foreach(QChar cr, str)
        {
            if(cr.toAscii() != QChar(0))
            {
                isAcsii = static_cast<QString>(cr.toAscii());
                strNew+=isAcsii;
            }
            else
            {
                tmp.setNum(cr.unicode());
                tmp.prepend("\\u");
                strNew+=tmp;
            }
        }
        QMessageBox::about(0,"Unicode escapes!",strNew);
        return app.exec();
    }
    

    感谢@danielearwicker的算法,当然还有+1。

    顺便说一句,您需要为文本编辑器编码指定UTF-8。

        5
  •  0
  •   Nains    15 年前

    您必须首先确定文本“Hello”使用哪种编码°", 看起来像俄国人,可能是Win代码页1251。或者UTF-8或者其他什么。

    希望有帮助。

        6
  •  0
  •   Sb0y    9 年前

    我的解决方案:

    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 ) );
    

    用俄语正确。 变换

    hello
    привет
    

    进入之内

    hello
    \\u043F\\u0440\\u0438\\u0432\\u0435\\u0442