In C, I would like to convert a signed char to an int, without sign extension. So if the signed char is 0xFF, the int would also be 0xFF. Simply casting to an int won’t work; the result would be 0xFFFFFFFF (on a 32-bit machine).
This seems to work (and is already pretty simple):
int convert(signed char sc) {
return 0xFF & (int) sc;
}
But is there a simpler or more idiomatic way?
Edit: Fixed function
You can cast to
unsigned charfirst. Assuming a definition:You could just do: