Is there a way to directly read a number from file to unsigned char variable?

68 views Asked by At

As in reading "117" in a text file and giving the variable value 'u', not '1'. And I know I can do it simply and quickly by reading the value to an int and then casting it to unsigned char, I just need to know if I can do it directly.

1

There are 1 answers

0
Stephan Lechner On

Try scanf with format specifier hhud (cf. scanf format specifiers at cplusplus.com)

unsigned char c;
scanf("%hhud", &c);

In the terminal, I enter 117, and variable c then shows value 'u' in the debugger...