PDA

View Full Version : Part of a QByteArray() to QString()



elcuco
4th November 2010, 19:00
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:


// QByteArray byte_array, int start_offset, int count
QString s;
for ( int i=0; i<count; i ++ )
s.append( byte_array[ start_offset + i ] );


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


s = byte_array.truncate( start_offset, count ).toLatin1();

sirius
4th November 2010, 20:09
Not truncate, but mid:



QString s;
s = QString::fromLatin1(byte_array.mid(10, 5));


The QByteArray doc is a great place to look for answers :rolleyes: