Results 1 to 4 of 4

Thread: UTF8 to ISO 8859... conversion

  1. #1
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default UTF8 to ISO 8859... conversion

    The file I try to load is UTF8 encoded - I'm doing this with:

    Qt Code:
    1. QTextStream in(&file);
    2. QString fs = in.readAll();
    To copy to clipboard, switch view to plain text mode 

    How can I convert this string into an other encoding? I tried something like this, but don't get it work:

    Qt Code:
    1. QTextCodec *codec = QTextCodec::codecForName("ISO 8859-1");
    2. QByteArray ba = codec->fromUnicode(fs);
    3. QString vcflatin1 = ba;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    The application I want to build is a conversion tool wich reads several VCF (address) files to a CSV file.

    Thanks for any help!
    Claudio

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UTF8 to ISO 8859... conversion

    QString is an Unicode string, so you can't convert it into another encoding. Instead you have to tell QTextStream which encoding it should use by invoking QTextStream::setCodec().

  3. #3
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: UTF8 to ISO 8859... conversion

    So how can I convert the QString 'fs' - wich is already in UTF8 - to an other QString with an other encoding? Sorry for this newbie question...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UTF8 to ISO 8859... conversion

    Quote Originally Posted by claudio View Post
    So how can I convert the QString 'fs' - wich is already in UTF8 - to an other QString with an other encoding? Sorry for this newbie question...
    You can't. QString can be only in UTF-16. However you can use whatever encoding you like while writing it to a file/stream/QByteArray.

    When you read something from a file using QTextStream, you have to tell QTextStream which codec to use (if you don't like the default one) and in return you will get QStrings in UTF-16. Then you can write them to another file using different QTextStream with a different codec.

    The proper workflow in Qt is:

    file -> codec -> QString -> codec -> file

    not:

    file -> QString -> codec -> QString -> file

  5. The following user says thank you to jacek for this useful post:

    claudio (30th December 2007)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.