QVariant to int8 and uint8_t
-
Hello
I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
e.gnationality = rec.value(rec.indexOf("nationality")).toUInt();
but i get this
warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
so should i use static cast here ? or there's another way .
-
Hello
I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
e.gnationality = rec.value(rec.indexOf("nationality")).toUInt();
but i get this
warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
so should i use static cast here ? or there's another way .
@MrLibya said in QVariant to int8 and uint8_t:
so should i use static cast here ? or there's another way .
will result in the same warning.
the variablenationality
should be of the of the target type (uint
) instead ofint8_t
, thats what this warning means -
Hello
I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
e.gnationality = rec.value(rec.indexOf("nationality")).toUInt();
but i get this
warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
so should i use static cast here ? or there's another way .
@MrLibya The warning clearly states, that your destination data type is too small to take values an
uint
can hold. Also, you mix signed and unsigned integers.The very correct way would be (assuming
nationality
isuint8_t
:bool ok = false; uint value = rec.value(rec.indexOf("nationality")).toUInt(&ok); if (ok && value < 255) nationality = uint8_t(value); // or static_cast<uint8_t> else nationality = 0;
If
nationality
is signed, i.e.int8_t
(does that make sense?!), you should usetoInt()
instead and adjust the range checks.Regards
-
@MrLibya said in QVariant to int8 and uint8_t:
so should i use static cast here ? or there's another way .
will result in the same warning.
the variablenationality
should be of the of the target type (uint
) instead ofint8_t
, thats what this warning means@raven-worx
i know what's that mean but this varable can't get value more then 127 so i don't want use uint for small values . -
@MrLibya The warning clearly states, that your destination data type is too small to take values an
uint
can hold. Also, you mix signed and unsigned integers.The very correct way would be (assuming
nationality
isuint8_t
:bool ok = false; uint value = rec.value(rec.indexOf("nationality")).toUInt(&ok); if (ok && value < 255) nationality = uint8_t(value); // or static_cast<uint8_t> else nationality = 0;
If
nationality
is signed, i.e.int8_t
(does that make sense?!), you should usetoInt()
instead and adjust the range checks.Regards
-
@aha_1980
the value from the db always postive and less then 127 so i only need to read it from the db but the function dosen't have toInt8 so this why i use toUInt , i'm asking is there a function for Int8 or i only should use static cast .@MrLibya said in QVariant to int8 and uint8_t:
the value from the db always postive and less then 127
Then either use
uint
or the approach I explained above. There is no need to use an signed integer likeint8_t
l -
@MrLibya said in QVariant to int8 and uint8_t:
the value from the db always postive and less then 127
Then either use
uint
or the approach I explained above. There is no need to use an signed integer likeint8_t
l -
@MrLibya said in QVariant to int8 and uint8_t:
like why i use 4 Bytes and i can use 1 Byte ?
Do you use them in an array or another struct? If not due to the alignment it will at least use 32 bits for it.
Don't try to optimize without measuring first. -
@MrLibya said in QVariant to int8 and uint8_t:
like why i use 4 Bytes and i can use 1 Byte ?
Do you use them in an array or another struct? If not due to the alignment it will at least use 32 bits for it.
Don't try to optimize without measuring first.@Christian-Ehrlicher
i'm using them in class
-
First your class members are not optimal aligned for low memory usage so why do you care for this specific member and secondly - this class is so big that a max of 3 more bytes don't count in any way.
-
First your class members are not optimal aligned for low memory usage so why do you care for this specific member and secondly - this class is so big that a max of 3 more bytes don't count in any way.
@Christian-Ehrlicher
ok thanks :)