×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    27

    How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    Read data from UTF-8 formatted XML file, and want to introduce converted UCS2-formatted parameters into Symbian functions.
    I tried many ways with QString, but none meets my need perfectly.
    Can someone help me?

    QString strContent_utf8; is XML node value.
    Code:
        // QString toUtf8() utf16() unicode() toLocal8Bit()
        // all tried, but none work well directly
        QString strContent;
        char    bufUtf8[256];
        int     lenUtf8 = snprintf( bufUtf8, sizeof(bufUtf8), "%s", strContent_utf8.toAscii().constData() );
        strContent.fromUtf8( bufUtf8 );
    
        // should continue to handle here
        // but use QMessageBox to show the content
        //     strContent_utf8 is utf8 string
        //     strContent is empty
    Code:
        // directly use without snprint, pointer will be modified
        // ??? - it is OK under Windows Mobile
        // const char* pcContent_utf8 = strContent_utf8.toAscii().constData();
        char    bufUtf8[256];
        int     lenUtf8 = 0;
        lenUtf8 = snprintf( bufUtf8, sizeof(bufUtf8), "%s", strContent_utf8.toAscii().constData() );
        TPtrC8    ptrContent_utf8( (const TUint8*)( bufUtf8 ), lenUtf8 );
        TBuf16<256>	strContent;
        CnvUtfConverter::ConvertToUnicodeFromUtf8( strContent, ptrContent_utf8 );
    
        // the strContent is well UCS2-formatted
        // it works well, but
        //     1. have to use Symbian encode/decode API
        //     2. have to use temp resource
        //     3. not introduce UCS2-formatted parameters into Symbian functions directly

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    Hi,

    I think I'm missing some information to help here. What is strContent_utf8? Is it another QString? QString's store data as UCS2 internally - the use of toAscii() looks very wrong to me.

    Mark

    P.S. To go from a QString to a Symbian descriptor I'd recommend using RBuf as the descriptor, initialize it with the QString unicode() and length() methods (assuming you've got a valid QString there in the first place) and make sure the QString stays valid until after you're finished with the descriptor.
    Last edited by Sorcery-ltd; 2009-02-18 at 10:04.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    27

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    Mentioned above:
    QString strContent_utf8; is XML node value, UTF-8 formatted.

    Use toAscii(), because I found none works correctly: toUtf8() utf16() unicode() toLocal8Bit().
    Use toAscii() and Symbian APIs, it works well.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    Hi,

    I think your error is before the code you've posted. How are you populating a QString with UTF-8 data?

    You either want to keep the UTF-8 in a QByteArray or just use the fromUtf8() static member to construct the QString in the first place.

    Mark

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    27

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    I use QDomDocument/QDomElement to read data from UTF-8 formatted data.
    And, introduce the node value to the Symbian APIs.

    QDomDocument xmlTree;
    QDomElement rootNode = xmlTree.documentElement();
    QDomElement nodeMsg = rootNode.firstChildElement( "Msg" );

    QString strContent_utf8 = nodeMsg.attribute("content");

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    I'm not sure exactly what you're trying to do but I think what you really want is either:
    Code:
    nodeMsg.text();
    or more likely:
    Code:
    QDomAttr attr = nodeMsg.attributeNode("content");
    QString content = attr.value();
    It's not entirely clear to me from the docs what QDomElement::attribute() returns but if the content is in a QString then it should have already been converted from UTF-8, since QString objects use Unicode internally. By using the toAscii method you turn it back into an 8-bit string and possibly lose some characters.

    Mark

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    27

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    It go back now!

    In QT Document for QDomElement:
    QString QDomElement::attribute ( const QString & name, const QString & defValue = QString() ) const
    Returns the attribute called name. If the attribute does not exist defValue is returned.
    If it isn't what I think, I couldn't read the data. But I did.

    The reason I use toAscii() and local Symbian C++ API is the string format is not UCS2, what I want to use directly. I have tried utf16(), unicode(), toLocal8Bit(), but none works well. I just want to know which one should I use.

    The only way is to write a test case.

    So, let's stop here if no test code written and test finished.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    OK, if you've got something that works then leave it. In theory I believe the answer to your question should be that you're supposed to use unicode() but if that isn't working then, if it were my project I'd investigate why (step through it in a debugger and see what actually happens to the string). However, I seem to recall that you don't use Carbide.c++ or a debugger so that makes investigating the internals a little more tricky.

    Mark

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    27

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    No matter what method I use, it can be tested without IDE in GUI application.
    I use CppUnit to test Non-GUI program.
    I have no interest to debug into the source code of QT, I just want to use it.
    And here, I use QMessageBox to show the string.

    I tested both S60 and Windows Mobile.
    It's the same.

    The original content is "test SMS", here "test" is in Chinese.
    When I directly use the QString: nodeMsg.attribute("content"), it shows #$!@ SMS.
    When I use QString((const QChar*)(strContent.PtrZ()), lenContent) after S60/WM local API transferm, it works well.
    TBuf16 strContent[256] under S60;
    TCHAR strContent[256] under Windows Mobile, of course no PtrZ().

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    2,280

    Re: How to use QDomDocument/QString between UTF8 and UCS2 correctly?

    Hi,

    Your choice of tools is up to you. The point I'm trying to make is that there is probably a bug somewhere, maybe there's something wrong with the formatting of the XML, possibly there is a bug in the Qt libraries.

    Some engineers like to understand these things, others just want the quickest workaround. In my opinion, the second type are really just storing up problems for later - not in every case, often you get away with it, but sometimes it comes back to bite you. I prefer to work with the first type of engineer!

    Your project, your choice - I was just trying to help.

    Mark

    P.S. You see the content of the QString SHOULD be UCS-2 internally!

Similar Threads

  1. Converting UTF8 to UCS2
    By nadav_smilansky in forum Symbian
    Replies: 2
    Last Post: 2009-01-18, 16:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×