Results 1 to 6 of 6

Thread: how to dump a QByteArray to A QString with one sentence?

  1. #1
    Join Date
    Mar 2007
    Posts
    7
    Thanks
    1

    Default how to dump a QByteArray to A QString with one sentence?

    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!

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: how to dump a QByteArray to A QString with one sentence?

    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?
    Last edited by Kumosan; 16th March 2007 at 07:07. Reason: updated contents

  3. #3
    Join Date
    Mar 2007
    Posts
    7
    Thanks
    1

    Default Re: how to dump a QByteArray to A QString with one sentence?

    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".

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: how to dump a QByteArray to A QString with one sentence?

    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.

  5. #5
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to dump a QByteArray to A QString with one sentence?

    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.

    Quote Originally Posted by csoapy View Post
    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);
    Last edited by camel; 16th March 2007 at 08:30. Reason: Use enums

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to dump a QByteArray to A QString with one sentence?

    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.

Similar Threads

  1. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  2. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. [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.