Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint32_t GetHex(void) {
- #define MAXIN 4096 /* MAXIN is the maximum number of input characters */
- static char input[MAXIN]; /* declared as static so it's not on the stack */
- char last;
- uint32_t result;
- fgets(input,MAXIN,stdin); /* fgets as scanf() can't handle blank lines */
- /* First of all check if it was a blank line, i.e. just a '\n' input...*/
- if ('\n' == input[0]) return(INERR); /* ...which is invalid input too */
- /* Now sscanf() can be used to parse a hex # but we still need to detect
- extraneous input _after_ a valid hex #. has been parsed, hence the %c
- that should have '\n' assigned to it. So, if a valid hex # is parsed
- _and_ there was at least one more character - which there should be -
- then sscanf() will return 2 - the number of successfully parsed items */
- if ((sscanf(input,"%8x%c",&result,&last) < 2)) return(INERR);
- /* Now check to make sure the last character input was a '\n', if not... */
- if ('\n' != last) return(INERR); /* ...then return an input error value */
- /* If we get to this point it has to be a valid hex # so just return it. */
- return(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement