Results 1 to 12 of 12

Thread: QString iso 8859-1 conversion

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.
    Qt Code:
    1. QString stream = "èèèèèèèèèèèèè";
    2. QString latinStream;
    3. for ( int i = 0; i < stream.size(); i++ )
    4. {
    5. latinStream.append( stream.at(i).toLatin1() );
    6. }
    To copy to clipboard, switch view to plain text mode 
    any hint?
    thx

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by mattia View Post
    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 View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by jacek View Post
    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.
    There are 10 people in the world. Those who understand binary and those who don't.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by Thomas View Post
    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.

  7. #7
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by jacek View Post
    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...

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by mattia View Post
    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().

  9. #9
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString iso 8859-1 conversion

    I'm still here trying to fix this problem...
    Qt Code:
    1. QString string = "èèèèèè";
    2. QTcpSocket socket;
    3. QTextStream inSendMessage ( & socket );
    4. inSendMessage << string.toLatin1() << endl;
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by mattia; 16th January 2008 at 11:26.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString iso 8859-1 conversion

    Are you sure you create that QString properly? What encoding do you use for "èèèèèè"?

  11. #11
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString iso 8859-1 conversion

    Quote Originally Posted by mattia View Post
    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().

Similar Threads

  1. UTF8 to ISO 8859... conversion
    By claudio in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2007, 21:24
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. conversion from QString to const uint8 *
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2007, 13:14
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.