Is there an easy way to convert between char
and unsigned char
if you don't know the default setting of the machine your code is running on?
(On most architectures, char
is signed by default and thus has a range from -128
to +127
. On some other architectures, such as ARM, char
is unsigned by default and has a range from 0 to 255)
I am looking for a method to select the correct signedness or to convert between the two transparently, preferably one that doesn't involve too many steps since I would need to do this for all elements in an array.
Using a pre-processor definition would allow the setting of this at the start of my code.
As would specifying an explicit form of char
such as signed char
or unsigned char
as only char
is variable between platforms.
The reason is, there are library functions I would like to use (such as strtol
) that take in char as an argument
but not unsigned char
.
I am looking for some advice or perhaps some pointers in the right direction as to what would be a practical efficient way to do this to make the code portable, as I intend to run the code on a few machines with different default settings for char
.