How to get uint8_t from a QByteArray
-
Hello,
I have the following trouble: I have a QSerialPort that read the messages from a Pixhawk (a component that transmit messages in a Hexadecimal base, with values from 0 to 255). When I read the messages I store this datas in a QByteArray, that has the following apparence:
At this point, I passed this QByteArray to other one with the command toHex and I get this QByteArray:
But now I want to access to the byte of the original messages thought the last one QByteArray. I know that the information in the message is store in every two characters, so every byte is a number that has some kind of meaning that I can translate with a library called MavLink.
However I dont know how to access to one byte from the last QByteArray, i can access to the chars and the I can append this char to a string and then get to value in a int though toInt. But with this method I have two troubles: its really heavy and, overall, in some time I will need to construct variables uint16_t or uint64_t and I dont know what to do to get it.int pasarNumeroaINT8 (QByteArray as, int i1){ char prim=as.at(i1), sec=as.at(i1+1); QString byte = ""; byte.append(prim); byte.append(sec); bool ok; int byteInt = byte.toInt(&ok, 16); return byteInt; }
I tried other method, and it was make it with the original QByteArray and a static cast, but I dont get values between 0 and 255, but -128 and 128.
data = serial->readAll(); dataAux = data.toHex(); qDebug()<<static_cast<int8_t>(data[0]);
Anyone can help me? Thanks you.
Sorry for my English.
-
@Dooham said in How to get uint8_t from a QByteArray:
but -128 and 128.
Because you cast to int8_t ...
-
@Christian-Ehrlicher Thanks, that was exactly my problem.