PDA

View Full Version : UTF8 to ISO 8859... conversion



claudio
29th December 2007, 20:59
The file I try to load is UTF8 encoded - I'm doing this with:


QTextStream in(&file);
QString fs = in.readAll();

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


QTextCodec *codec = QTextCodec::codecForName("ISO 8859-1");
QByteArray ba = codec->fromUnicode(fs);
QString vcflatin1 = ba;
...

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

jacek
29th December 2007, 21:05
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().

claudio
29th December 2007, 21:11
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...

jacek
29th December 2007, 21:24
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