Results 1 to 2 of 2

Thread: QString::toLocal8Bit() on UTF-8 locales - does not produce UTF-8

  1. #1
    Join Date
    Oct 2009
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default QString::toLocal8Bit() on UTF-8 locales - does not produce UTF-8


    I use QString::toLocal8Bit() on Linux with a UTF-8 locale (eg, fr_FR.UTF-8, en_US.UTF-8). What is strange is that I do not get the same results as with QString::toUtf8(). For example, the following code:

    Qt Code:
    1. #include <QString>
    2. #include <iostream>
    4. int main(int, char **) {
    5. QByteArray array = QString::fromUtf8("\xc3\xa9").toLocal8Bit();
    6. std::cout << array.constData();
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    outputs a single byte E9 (in hexadecimal). If toLocal8Bit is replaced with toUtf8, the program outputs the sequence of bytes C3 A9 as expected.

    C3 A9 and E9 are the encodings of "é" in UTF-8 and Latin-1 respectively. In other words, toLocal8Bit() outputs Latin-1 in this case, although the locale is UTF-8.

    This is quite a problem, as I rely on QString::toLocal8Bit() to pass filenames to system calls for example. Has anyone come accross this issue before? Is there any reliable alternative?

  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: QString::toLocal8Bit() on UTF-8 locales - does not produce UTF-8

    As far as I remember toLocal8Bit() "almost always" returns latin1.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.