Why is this hexadecimal value obtained as a negative number?

char buffer_b[5] = { 0xDA, 0x00, 0x04, 0x00, 0x07 };
printf("%d\n%d\n%d", buffer_b[0], buffer_b[2], buffer_b[4]);

This gives me the conclusion:

-38
4
7

However, I expect:

218
4
7

Thank.

+3
source share
3 answers

char. Use unsigned char.

use% ud as well.

+8
source

, char . ( , , .) , , - 0xDA, , . printf, () char int . %d, printf .

unsigned, %u. unsigned, unsigned char uint8_t, printf printf.

+8

char 0xDA int, printf, , 0xffffffda, 32- -38. , 0x000000da. , , signed char unsigned char. , .

, - char, .

+2
source

Source: https://habr.com/ru/post/1742488/


All Articles