PDA

View Full Version : how to dump a QByteArray to A QString with one sentence?



csoapy
16th March 2007, 07:56
e.g.

A QByteArray is "0x10, 0x00, 0xFF"

i want to change it into one QString object:
1000FF,
10 00 FF
10,00,FF
0x10, 0x00, 0xFF
or
16, 0, 255

and so on.

ths!

Kumosan
16th March 2007, 08:02
Nothing much you can do than iterate over your whole array and build your string yourself.

On further thought: You see that QString has a constructor, which takes a QByteArray. This is worthless for you when your array can contain a 0 character, which it can in your example. But perhaps QString & append ( const QByteArray & ba ) does not have this restriction?

csoapy
16th March 2007, 08:37
Thank you very much.
the reply so quickly!

i trid to:

QByteArray buf;
buf.append('A');
buf.append((char)0);
buf.append('B');
this->setWindowTitle(QString(buf));

then, the title is "A", so only one (char)0 in QString that is "end of this string".

Kumosan
16th March 2007, 08:59
Was to be expected, but one never knows if one does not try. :-)

The only problem you have seem to be the 0 values. Maybe you could replace them in your QByteArray? Or remove them?

If not you really have to write code to iterate over your array. But this really isn't hard.

camel
16th March 2007, 09:06
In the general case I would go with QString::fromAscii() or its brothers (fromLatin1...), these take a char* and a length, thus they should not have the "\0" problem.



i trid to:

QByteArray buf;
buf.append('A');
buf.append((char)0);
buf.append('B');
this->setWindowTitle(QString(buf));

then, the title is "A", so only one (char)0 in QString that is "end of this string".

My Question would be: What kind of a result do you like to have?

The problem is (probably) that the window title itself is not inside of Qt's scope. It is handled (in general) by the window manager. So while QString is ok with the internal "\0", the window manager might not be, doing a strlen on the title for example. In this case there is nothing you can do, besides calling QString::replace and changing the 0 to for example the "missing symbol" unicode symbol (aka. Replacement Character)...i.e.


QString string;
string.replace(QChar::Null, QChar::ReplacementCharacter);

wysota
16th March 2007, 09:18
QString keeps its data unicode encoded. Are you sure you need that? What's wrong with keeping the data in QByteArray (3 bytes) instead of QString (6 bytes)? QString is not really meant to keep unprintable characters.