PDA

View Full Version : QString iso 8859-1 conversion



mattia
7th January 2008, 10:13
Hello, i'd like to convert a QString with iso 8859-1 conversion, cos when i send it through a tcp connection i can't receive it formatted as i want but it is composed by strange character.
I tried with this code, but it does not work.


QString stream = "èèèèèèèèèèèèà ¨";
QString latinStream;
for ( int i = 0; i < stream.size(); i++ )
{
latinStream.append( stream.at(i).toLatin1() );
}

any hint?
thx

jacek
7th January 2008, 13:46
QStrings are Unicode strings. You can't create a QString that uses ISO-8859-1 encoding. If you want to use a certain encoding, you will have to switch to QByteArray.

How do you send that string?

mattia
7th January 2008, 15:05
i'm sending it through a simple tcp socket connection...
to convert it i have to put my string into a QByteArray and send it?
Can't i just have a QString with the text converted in ?
thx

jacek
7th January 2008, 15:42
to convert it i have to put my string into a QByteArray and send it?
If you use only QTcpSocket, you need QByteArray anyway. Try QString::toLatin1() or QString::toLocal8Bit().


Can't i just have a QString with the text converted in ?
No, because QString isn't an array of bytes, but an array of QChars and QChars are Unicode characters.

Thomas
7th January 2008, 19:35
If you use only QTcpSocket, you need QByteArray anyway. Try QString::toLatin1() or QString::toLocal8Bit().


No, because QString isn't an array of bytes, but an array of QChars and QChars are Unicode characters.

Jacek, the conversion to the local locale works only if both, the server and the client, use the same Locale.

I really wonder why Mattia receives junk. A TCP connection does not change byte values because it would not know how to do it. Mattia, could you please post a small example for the server/client which reproduces the problem without your latin1() conversion? The problem lies perhaps elsewhere.

jacek
7th January 2008, 20:08
I really wonder why Mattia receives junk.
Most likely because Qt sends QString in UTF-16, which means that 50% of characters are null, and the receiver expects a null-terminated string.

mattia
8th January 2008, 13:35
Most likely because Qt sends QString in UTF-16, which means that 50% of characters are null, and the receiver expects a null-terminated string.
What should i do to avoid this?
my server/client is almost like a simple server/client that you can find in a qt tutorial...

jacek
8th January 2008, 14:22
What should i do to avoid this?
my server/client is almost like a simple server/client that you can find in a qt tutorial...
You can use QString::toLatin1(), if the receiver expects ISO-8859-1 string. If your protocol is text-based, you can use QTextStream and set the encoding using QTextStream::setCodec().

mattia
16th January 2008, 10:28
I'm still here trying to fix this problem...


QString string = "èèèèèè";
QTcpSocket socket;
QTextStream inSendMessage ( & socket );
inSendMessage << string.toLatin1() << endl;

I receive strange char...maybe does the socket do an ASCII 7bit conversion?

The server is developed with Qt3...maybe there is a charset encoding problem between them?

jacek
16th January 2008, 15:18
Are you sure you create that QString properly? What encoding do you use for "èèèèèè"?

mattia
21st January 2008, 13:36
to know what codec i'm using have i to use QTextCodec::codecForCStrings() ?
but it's return a *QTextCodec ... isn't there a sort of toString() method to print the codec name?

jacek
21st January 2008, 14:17
but it's return a *QTextCodec ... isn't there a sort of toString() method to print the codec name?
You can try QTextCode::name() or QTextCodec::aliases(), but better simply set the right codec using QTextCodec::setCodecForCStrings().