Advertisement
Guest User

Untitled

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