How to convert QString to unsigned char in Qt
-
Hi Team,
I am new to Qt and am trying to understand Qt conversions (like QString to unsigned char, QString to unisgned short, etc..)I have a C code like below
unsigned short us = -1; printf("us = %d\n", us);
The output is 65535 which is as expected (the data type range roll over happens here).
Same thing, I have tried with Qt i.e., converting QString to unsigned char or unsigned short like below
QString str = "-1"; qDebug() << "value = " << str.toUShort();
The output is 0.
How can I get the output as 65535 in Qt ?
-
Hi Team,
I am new to Qt and am trying to understand Qt conversions (like QString to unsigned char, QString to unisgned short, etc..)I have a C code like below
unsigned short us = -1; printf("us = %d\n", us);
The output is 65535 which is as expected (the data type range roll over happens here).
Same thing, I have tried with Qt i.e., converting QString to unsigned char or unsigned short like below
QString str = "-1"; qDebug() << "value = " << str.toUShort();
The output is 0.
How can I get the output as 65535 in Qt ?
@Praveen-Illa said in How to convert QString to unsigned char in Qt:
QString str = "-1";
toUshort()
fails with negative number strings (if you use theok
argument, it will indeed returnfalse
), so you need to convert to a signed int as an intermediate step first and cast it afterwards:QString str = "-1"; bool OK; ushort val = str.toInt(&OK); qDebug() << val << OK;