Qt Convert a negative hexadecimal string to integer labels

I am reading the registers of the i2c device, and the range of the return value is from 32768 to 32768, signed integers. The following is an example:

# i2cget -y 3 0x0b 0x0a w
0xfec7

In Qt, I get this value (0xfec7) and want to display it in QLabel as a signed integer. The variable stringListSplit [0] is a QString with the value "0xfec7".

// Now update the label
int milAmps = stringListSplit[0].toInt(0,16); // tried qint32
qDebug() << milAmps;

The problem no matter what I try, I always get unsigned integers, so for this example I get 65223, which exceeds the specified maximum return value. I need to convert a hexadecimal value to a signed integer, so I need to treat the hexadecimal value as expressed with 2s padding. I do not see a simple method in the QString documentation. How can I achieve this in Qt?

NOTE:

QString:: toShort 0:

// Now update the label
short milAmps = stringListSplit[0].toShort(0,16);
qDebug() << "My new result: " << milAmps;

stringListSplit [0], '0xfebe', -322, C-style casting, Keith :

// Now update the label
int milAmps = stringListSplit[0].toInt(0,16);
qDebug() << "My new result: " << (int16_t)milAmps;
+5
2

16- .

qDebug() << (int16_t)milAmps;
+3

16- . , QString::toShort.

short milAmps = stringListSplit[0].toShort(0,16); 
qDebug() << milAmps;
+4

All Articles