PDA

View Full Version : reading utf8 as data stream.



kami
20th September 2008, 23:51
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:


class FlashDataStream : public QDataStream
{
public:
FlashDataStream () : QDataStream() {}
FlashDataStream ( QIODevice * d ) : QDataStream(d) {}
FlashDataStream ( QByteArray * a, QIODevice::OpenMode mode ) : QDataStream(a, mode) {}
FlashDataStream ( const QByteArray & a ) : QDataStream (a ) {}

QString ReadUTF8(qint16 maxsize = 4096)
{
Q_ASSERT(maxsize <= 4096);

static char buffer[4096];
qint16 stringlength;
(*this) >> stringlength;

if (stringlength < maxsize)
{
readRawData(&buffer[0] , stringlength);
}
return QString::fromAscii(buffer, stringlength);
}

void WriteUTF8(QString data)
{
(*this) << data.size();
writeRawData((char*)data.data(), data.size());
}


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?

jacek
28th September 2008, 21:48
You could read the length and then use QTextStream.

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