Solved: How to include unicode in QString???



  • hello Gurus,

    I have the following code to replace the &#xxxx unicode decimal signs into \u0628 hexas. But the funny result is that

    When I give the string manually it is converted, when it gets from previous function it is not converted.
    Any body focus my mistake?

    @

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    
    qDebug() << "system name:"      << QLocale::system&#40;&#41;.name(&#41;;
    qDebug(&#41; << "codecForCStrings:" << QTextCodec::codecForCStrings()->name();
    qDebug() << "codecForLocale:"   << QTextCodec::codecForLocale()->name();
    
    
    // convert string from &#xxxx; format to /u hex format to be used for unicode
    
    int index; //QString name;
    
    QString name="&#1578;&#1578;&#1578;&#1579;&#1578;&#1573;&#1578;&#1572;&#1606;&#1601;&#1606;&#1606;&#1606;&#1606;&#1606;&#1606;&#1578;&#1606;&#1578;";  //bidi example
    int off=0;
    QString Outp;
    while(index>=0)
    {
    QRegExp [removed]"&#[0-9][0-9][0-9][0-9];"); //
    index=expression.indexIn(name, off);
    qDebug () << "index" << index;
    int length = expression.matchedLength();
    if (index==-1) break;
    QString v;
    v=name.mid(index+2,length-2-1);
    QString re;
    re=name.mid(index,length);
    
    qDebug() << "v=" &lt;&lt; v &lt;&lt; "re=" &lt;&lt; re;
    
    uint r=v.toInt(0,10);
    
    qDebug() &lt;&lt; "int=" &lt;&lt; r;
    
    uint decimal = r;
    QString haxadecimal;
    haxadecimal.setNum(decimal,16);
    
    if (haxadecimal.length()==3)
    {
        haxadecimal= "\\u0" + haxadecimal;
    }
    
    qDebug() << "hexa" << haxadecimal;
    
    name.replace(re,haxadecimal);
    qDebug() << "inloop output" << name;
    
    off=index+length-2;
    }
    
    qDebug () << "output" << name; // name = \u062a\u062a
    
    
    // now the problem is : When the output name is feeded to the next part
    // the qDebug is not shown in unicode format. However if the output is given
    // manually to the name string, then it is shown correctly
    
    QTextCodec::setCodecForCStrings("UTF-8");
    
    name = "\u062a\u062a"; // \u062a\u062b\u062a\u0625\u062a\u0624\u0646\u0641\u0646\u0646\u0646\u0646\u0646\u0646\u062a\u0646\u062a";
    
    
    QByteArray utf8;
    utf8.append(name);
    qDebug() << "utf8" << name;  // << utf8.toHex();
    
    QByteArray latin1;
    latin1.append(name.toLatin1());
    qDebug() << "Latin1" << name ;  //<< latin1.toHex();
    
    QTextCodec *codec = QTextCodec::codecForName("latin1");
    QByteArray encodedString = codec->fromUnicode(name);
    qDebug() << "ISO 8859-1" << name; // << encodedString.toHex();
    

    @



  • solved:

    QChar a=QChar::Qchar(hexanumber of the character)

    Then add the QChar into a string


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.