Results 1 to 2 of 2

Thread: Part of a QByteArray() to QString()

  1. #1
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Part of a QByteArray() to QString()

    In my situation I have a QByteArray which I get from a UDP socket. I know that from bytes 10 I have up to 5 bytes which represent some name (guaranteed to be latin1).

    My code currently reads:
    Qt Code:
    1. // QByteArray byte_array, int start_offset, int count
    2. for ( int i=0; i<count; i ++ )
    3. s.append( byte_array[ start_offset + i ] );
    To copy to clipboard, switch view to plain text mode 

    I was wondering if there is a better way. Something like this code (I would assume it would check boundaries and it will use less memory cause of less copies).
    Qt Code:
    1. s = byte_array.truncate( start_offset, count ).toLatin1();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Location
    Norway
    Posts
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Part of a QByteArray() to QString()

    Not truncate, but mid:

    Qt Code:
    1. s = QString::fromLatin1(byte_array.mid(10, 5));
    To copy to clipboard, switch view to plain text mode 

    The QByteArray doc is a great place to look for answers
    !sirius

  3. The following user says thank you to sirius for this useful post:

    elcuco (4th November 2010)

Similar Threads

  1. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  2. QByteArray to QString
    By hvitual in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2009, 10:43
  3. QByteArray to QString
    By babu198649 in forum Newbie
    Replies: 7
    Last Post: 6th December 2007, 13:08
  4. How to get QByteArray from QString in Qt3
    By joseph in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2007, 09:23
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

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.