Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
769 B

  1. #include "scan.h"
  2. int scan_fromhex(unsigned char c) {
  3. c=(unsigned char)(c-'0');
  4. if (c<=9) return c;
  5. c=(unsigned char)(c&~0x20);
  6. c=(unsigned char)(c-('A'-'0'));
  7. if (c<6) return c+10;
  8. return -1;
  9. /* more readable but creates worse code:
  10. if (c>='0' && c<='9')
  11. return c-'0';
  12. else if (c>='A' && c<='F')
  13. return c-'A'+10;
  14. else if (c>='a' && c<='f')
  15. return c-'a'+10;
  16. return -1;
  17. */
  18. }
  19. #ifdef UNITTEST
  20. #include <assert.h>
  21. int main() {
  22. int i;
  23. for (i=0; i<256; ++i) {
  24. if (i>='0' && i<='9')
  25. assert(scan_fromhex(i)==i-'0');
  26. else if (i>='a' && i<='f')
  27. assert(scan_fromhex(i)==i-'a'+10);
  28. else if (i>='A' && i<='F')
  29. assert(scan_fromhex(i)==i-'A'+10);
  30. else
  31. assert(scan_fromhex(i)==-1);
  32. }
  33. return 0;
  34. }
  35. #endif