PDA

View Full Version : Convert QbyteArray to int return 0 (problem)



AUDI_ENG
8th July 2014, 11:08
Hello,

I convert a QByteArray to int using the toInt() function
So when I try to display the int value, its return 0
I'm trying also toUInt()



QByteArray* ByteID = new QByteArray;
ByteID = oneFrame.mid(3,1);
qDebug() << "ByteID = " << (*ByteID).toHex(); // return 0A --> working perfectly
qDebug() << "ByteID = " << (*ByteID).toUInt(); // return 0


I'm serching on Google but I don't find a correct response

stampede
8th July 2014, 11:21
Better read documentation than google :)


// example from QByteArray::toInt docs:
QByteArray str("FF");
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false

AUDI_ENG
8th July 2014, 11:27
Better read documentation than google :)


// example from QByteArray::toInt docs:
QByteArray str("FF");
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false


I readed documentation , I'm trying this code, nothing changed, I have 0 as result (for dec)


nb data read = 11 Data readed = "aaaa0c0e09090a0809090a"


*ByteID = oneFrame.mid(2,1);
int dec = (*ByteID).toInt(&ok,16);

ByteID_hex = "0c"
dec = 0

^NyAw^
8th July 2014, 11:35
Hi,

"FF" is not a valid value in decimal representation but is a valid value in hexadecimal. This is what "ok" variable is telling you.

AUDI_ENG
8th July 2014, 11:41
Hi,

"FF" is not a valid value in decimal representation but is a valid value in hexadecimal. This is what "ok" variable is telling you.

Hi,
Ok thank you, So How I can convert the QbyteArray to a valid decimal value ?

Lesiok
8th July 2014, 12:11
But QByteArray::toInt works with "string equivalent of the number". 0x0C is NOT valid ASCII digit.

AUDI_ENG
8th July 2014, 12:16
Thank you for this clear response
So, Qt has another function to convert a QByteArray to Int?

Lesiok
8th July 2014, 12:49
In example look at this thread (http://www.qtcentre.org/threads/29598-Extracting-int-values-from-QByteArray)

AUDI_ENG
8th July 2014, 13:20
In example look at this thread (http://www.qtcentre.org/threads/29598-Extracting-int-values-from-QByteArray)

Thank you So MUCH Lesiok :)

it's working :


int dec = static_cast<quint8>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe

Lesiok
8th July 2014, 13:28
You should write like this :
int dec = static_cast<int>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe
Why do two conversions ?

AUDI_ENG
8th July 2014, 13:32
You should write like this :
int dec = static_cast<int>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe
Why do two conversions ?

I don't know, can you explain me ? (where are the two convertions) ?

Lesiok
8th July 2014, 13:46
In Yours code :
1. First explicit conversion static_cast<quint8>
2. Second implicit conversion (by compiler) from quint8 to int (type of variable).

AUDI_ENG
8th July 2014, 14:10
In Yours code :
1. First explicit conversion static_cast<quint8>
2. Second implicit conversion (by compiler) from quint8 to int (type of variable).

OK :) Thank you