Qt-interest Archive, December 2006
Unicode Utf8 Problem
Message 1 in thread
Hello,
I have the following Problem (installed: Qt 4.1.4)
I want to store Unicode Utf8 encoded Strings in a File.
With the following code example I expected to do so:
------------------------------
QFile file("Utf8.txt");
if (!file.open(QIODevice::WriteOnly)) return;
QTextStream out(&file);
out.setCodec(QTextCodec::codecForName("UTF-8"));
QString tmp = QString::fromUtf8("aöüäA");
out << tmp.toUtf8();
file.close();
--------------------------
As result I expect the Utf8 representation of "aöüäA" in the file.
But when I open the File in a TextEditor with Utf8 Representation, this does not happen. (also the hex code of the result is not utf8)
When I do:
--------------------
out << tmp.toAscii(); // instead of:out << tmp.toUtf8();
-> the result is an utf8 representation. That does not make sense for me ...
---------------------
So could anybody give me a hint for that?
thanks,
Philipp
Message 2 in thread
On 07.12.06 15:14:58, Philipp Drechsler wrote:
> I have the following Problem (installed: Qt 4.1.4)
> I want to store Unicode Utf8 encoded Strings in a File.
> With the following code example I expected to do so:
> ------------------------------
> QFile file("Utf8.txt");
> if (!file.open(QIODevice::WriteOnly)) return;
> QTextStream out(&file);
> out.setCodec(QTextCodec::codecForName("UTF-8"));
> QString tmp = QString::fromUtf8("aöüäA");
> out << tmp.toUtf8();
> file.close();
> --------------------------
> As result I expect the Utf8 representation of "aöüäA" in the file.
> But when I open the File in a TextEditor with Utf8 Representation, this does not happen. (also the hex code of the result is not utf8)
Why do you use toUtf8? If you set the codec of the output stream it will
handle the encoding, just give the stream the QString directly. That
works here perfectly. Also make sure you save the C++ code in UTF-8.
Andreas
--
[ signature omitted ]
Message 3 in thread
> Why do you use toUtf8? If you set the codec of the output stream it will
> handle the encoding, just give the stream the QString directly. That
> works here perfectly. Also make sure you save the C++ code in UTF-8.
>
> Andreas
ok, I think I need to explain the problem, this small code snipped from me was
just a small example, how it does not work.... (not how I have implemented in
my software) --> but why does it not work?
I want to process linguist ts files and I need the output as utf8 again.
I checked the source file containing the "umlauts" with a hexeditor and they
are in utf8.
Perhaps somebody can help me ...
Regards,
Philipp
--
[ signature omitted ]
Message 4 in thread
On 08.12.06 09:02:12, Philipp Drechsler wrote:
> > Why do you use toUtf8? If you set the codec of the output stream it will
> > handle the encoding, just give the stream the QString directly. That
> > works here perfectly. Also make sure you save the C++ code in UTF-8.
> >
> > Andreas
> ok, I think I need to explain the problem, this small code snipped from me was
> just a small example, how it does not work.... (not how I have implemented in
> my software) --> but why does it not work?
Because you're encoding the text twice. QString::toUtf8 will give you a
byte-array that contains 2 bytes for each non-ascii character. Then
QTextStream gets this byte-array and as you told it to store the "text"
encoded in utf-8 it does just that, it encodes the QByteArray using the
utf-8 encoding scheme. This messes up your file, so as I said before do
not convert the QString to a utf-8 encoded byte-array before saving it
to the stream. You already told the stream to do the encoding.
BTW: The reason the QByteArray is not written "directly" to the file is
because you used a QTextStream, which has this:
QTextStream & QTextStream::operator<< ( const QByteArray & array )
This is an overloaded member function, provided for convenience.
Writes array to the stream. The contents of array are converted with QString::fromAscii().
> I want to process linguist ts files and I need the output as utf8 again.
> I checked the source file containing the "umlauts" with a hexeditor and they
> are in utf8.
No problem, Open the file, create a QTextStream, tell it to use UTF-8
encoding. Read the content into a QString. When saving do the same, a
file with a QTextStream set to utf-8 encoding and use
stream << yourstring;
to save the content to file.
Andreas
--
[ signature omitted ]