PDA

View Full Version : QString::toLocal8Bit() on UTF-8 locales - does not produce UTF-8



yeye_olive
1st October 2009, 18:03
Hello,

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:



#include <QString>
#include <iostream>

int main(int, char **) {
QByteArray array = QString::fromUtf8("\xc3\xa9").toLocal8Bit();
std::cout << array.constData();
return 0;
}


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?

wysota
1st October 2009, 21:28
As far as I remember toLocal8Bit() "almost always" returns latin1.