[Solved] What happened to operator ‘+’? [closed]


result[17] is a char value of 0x00.

result[18] is a char value of 0xC0.

Your compiler implements char as a signed type, so the compiler will sign-extend a char value to a signed int before assigning/adding it to an unsigned int variable. The high bit of 0xC0 is 1, so the extended bits are filled with 1s, producing 0xFFFFFFC0, which is 4294967232 when interpreted as an unsigned integer, and is -64 when interpreted as a signed integer.

To do what you are trying to do, use this instead:

iBienSuCo1 = (unsigned char)result[17];
iBienSuCo1 = (iBienSuCo1 << 8) | (unsigned char)result[18];

By casting char to unsigned char (not to unsigned int!) before extension, the compiler will zero-extend the values (the extended bits are filled with 0s) instead of sign-extend. That will produce the result of 0x000000C0 that you are looking for.

live demo

1

solved What happened to operator ‘+’? [closed]