QString iso 8859-1 conversion
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.
Code:
QString stream
= "èèèèèèèèèèèèè";
for ( int i = 0; i < stream.size(); i++ )
{
latinStream.append( stream.at(i).toLatin1() );
}
any hint?
thx
Re: QString iso 8859-1 conversion
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?
Re: QString iso 8859-1 conversion
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
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
mattia
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().
Quote:
Originally Posted by
mattia
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.
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
jacek
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.
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
Thomas
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.
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
jacek
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...
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
mattia
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().
Re: QString iso 8859-1 conversion
I'm still here trying to fix this problem...
Code:
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?
Re: QString iso 8859-1 conversion
Are you sure you create that QString properly? What encoding do you use for "èèèèèè"?
Re: QString iso 8859-1 conversion
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?
Re: QString iso 8859-1 conversion
Quote:
Originally Posted by
mattia
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().