Results 1 to 2 of 2

Thread: reading utf8 as data stream.

  1. #1
    Join Date
    Sep 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default reading utf8 as data stream.

    Hi,

    Flash 10, action script 4 sends strings this way:
    int16 describing size
    then char * data.

    To my knowledge, this type of string is not serialized in QT.

    so i made this class:
    Qt Code:
    1. class FlashDataStream : public QDataStream
    2. {
    3. public:
    4. FlashDataStream () : QDataStream() {}
    5. FlashDataStream ( QIODevice * d ) : QDataStream(d) {}
    6. FlashDataStream ( QByteArray * a, QIODevice::OpenMode mode ) : QDataStream(a, mode) {}
    7. FlashDataStream ( const QByteArray & a ) : QDataStream (a ) {}
    8.  
    9. QString ReadUTF8(qint16 maxsize = 4096)
    10. {
    11. Q_ASSERT(maxsize <= 4096);
    12.  
    13. static char buffer[4096];
    14. qint16 stringlength;
    15. (*this) >> stringlength;
    16.  
    17. if (stringlength < maxsize)
    18. {
    19. readRawData(&buffer[0] , stringlength);
    20. }
    21. return QString::fromAscii(buffer, stringlength);
    22. }
    23.  
    24. void WriteUTF8(QString data)
    25. {
    26. (*this) << data.size();
    27. writeRawData((char*)data.data(), data.size());
    28. }
    To copy to clipboard, switch view to plain text mode 

    Now, my question is; There has got to be a better way of reading the string than having to copy those bytes twice. Is there a way to achieve what i am doing in one operation?

  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: reading utf8 as data stream.

    You could read the length and then use QTextStream.

    Why do you use fromAscii() if you want to read UTF-8?

Similar Threads

  1. Replies: 7
    Last Post: 29th August 2008, 10:24
  2. Is it possible to stream data from device with phonon ?
    By Elder Orb in forum Qt-based Software
    Replies: 0
    Last Post: 24th July 2008, 19:58
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  4. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 10:58
  5. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 20:51

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.