unsigned char cTest = argv[1];
is wrong, because argv[1]
is of type char *
. If argv[1]
contains something like "0xff"
and you want to assign the integer value corresponding to that to an unsigned char
, the easiest way would be probably to use strtoul()
to first convert it to an unsigned long
, and then check to see if the converted value is less than or equal to UCHAR_MAX
. If yes, you can just assign to cTest
.
strtoul()
's third parameter is a base, which can be 0 to denote C-style number parsing (octal and hexadecimal literals are allowed). If you only want to allow base 16, pass that as the third argument to strtoul()
. If you want to allow any base (so you can parse 0xff
, 0377
, 255
, etc.), use 0.
UCHAR_MAX
is defined in <limits.h>
.