I would like to fscanf from a stream containing data like ABCD0000 into unsigned short variables.
What is the proper format specifier for that? I was unable to find any way of combining both "unsigned" and "hexadecimal" with the h (short) specifier.
Take for instance
#include <stdio.h>
unsigned short hex_input;
char byte;
int main(void) {
scanf("%hX", &hex_input);
printf("%hX\n", hex_input);
byte = hex_input >> 8;
printf("%hu\n", byte);
byte = (char)hex_input;
printf("%hu\n", byte);
return 0;
}
And the data AB00. What I get as output is:
AB00
65451
0
Note that the second line is wrong, I would expect 171 (the AB part).
Should be
hxstill. (Hexadecimal is handled as unsigned by default.) Also note that your example ofABCD0000will only have its lower four nybbles read into an unsigned short, so you’ll end up getting a value of0.