I have a binary value stored in a char in C, I want transform this byte into signed int in C.
Currently I have something like this:
char a = 0xff;
int b = a;
printf("value of b: %d\n", b);
The result in standard output will be “255”, the desired output is “-1”.
Replace:
by
to have
printfprints-1.If you don’t want to change the type of
a, as @veer added in the comments you can simply castato(signed char)before assigning its value tob.Note that in both cases, this integer conversion is implementation-defined but this is the commonly seen implementation-defined behavior.