You are probably storing the value 0xc0 in a char
variable, what is probably a signed type, and your value is negative (most significant bit set). Then, when printing, it is converted to int
, and to keep the semantical equivalence, the compiler pads the extra bytes with 0xff, so the negative int
will have the same numerical value of your negative char
. To fix this, just cast to unsigned char
when printing:
printf("%x", (unsigned char)variable);