Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QByteArray to integer

Tags:

bytearray

qt

As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.

 QByteArray buffer = server->read(8192);
 QByteArray q_size = buffer.mid(0, 2);
 int size = q_size.toInt();

However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.

The q_size is 0x2500 (or the other endianness order - 0x0025).

What's the problem here ? I'm pretty sure q_size holds the data I need.

like image 360
Ahmed Avatar asked Aug 09 '09 16:08

Ahmed


People also ask

How do you split QByteArray?

There is no "multi-char split" for a QByteArray . One of: Convert to QString and split if you can safely treat it as a string. Use one of the overloads of QByteArray::indexOf() in a loop to implement your own "split" (which ultimately is doubtless the code split() uses anyway).


1 Answers

bool ok;
q_size.toHex().toInt(&ok, 16);

works for me

like image 70
Antoine Nguyen Avatar answered Sep 17 '22 11:09

Antoine Nguyen