I am using array's of bytes and trying to understand better what happen during char to int conversion.
Here is the code :
#include <QDebug>
int main(int argc, char *argv[])
{
Q_UNUSED(argc);
Q_UNUSED(argv);
char val = 0xff; // 1111 1111
int a = val;
unsigned int b = val;
qInfo() << "size of int is " << sizeof(int);
qInfo() << a << " " << b << " " << 0xff ;
qInfo() << QString("0x%1 0x%2 0x%3").arg(a, 16, 16, QChar('0')).arg(b, 16, 16, QChar('0')).arg(0xff, 16, 16, QChar('0'));
return 0;
}
and the output :
size of int is 4
-1 4294967295 255
"0xffffffffffffffff 0x00000000ffffffff 0x00000000000000ff"
As my char has the value of 1111 1111b, i expected - but i was wrong - that the conversion would fill my array of 4 bytes with 0x0 0x0 0x0 0xff ( let's say bytes are BigEndian oriented but it doesn't matter here) as it seem to be the case for numeric literals like 0xff.
I know that 0xff can be interpreted as 255 or -1 in decimal (unsigned or signed integer with the first bit being used for the sign), nevertheless i still don't really understand the mechanism that gives this output.
EDIT : As pointed out in comments, i would like to understand the mechanism that "extends" from one byte to 4 bytes the signed char value. I don't ask why the char type is signed or unsigned on my platform but how the 3 remaining bytes are built, what are the rules behind this output.
Thanks.