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.
Asked
Active
Viewed 55 times
0
-
4No, I don't think so. – Barmar Jan 04 '17 at 20:33
-
This doesn't answer the question, but "casting it to unsigned char" isn't necessary; `int` is convertible to `unsigned char`, so no cast is needed. – Pete Becker Jan 04 '17 at 21:20
1 Answers
2
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...

Stephan Lechner
- 34,891
- 4
- 35
- 58