fork(10) download
  1. #include <stdio.h>
  2.  
  3. #define ASCII_0_VALU 48
  4. #define ASCII_9_VALU 57
  5. #define ASCII_A_VALU 65
  6. #define ASCII_F_VALU 70
  7.  
  8. unsigned int HexStringToUInt(char const* hexstring)
  9. {
  10. unsigned int result = 0;
  11. char const *c = hexstring;
  12. char thisC;
  13.  
  14. while( (thisC = *c) != NULL )
  15. {
  16. unsigned int add;
  17. thisC = toupper(thisC);
  18.  
  19. result <<= 4;
  20.  
  21. if( thisC >= ASCII_0_VALU && thisC <= ASCII_9_VALU )
  22. add = thisC - ASCII_0_VALU;
  23. else if( thisC >= ASCII_A_VALU && thisC <= ASCII_F_VALU)
  24. add = thisC - ASCII_A_VALU + 10;
  25. else
  26. {
  27. printf("Unrecognised hex character \"%c\"\n", thisC);
  28. exit(-1);
  29. }
  30.  
  31. result += add;
  32. ++c;
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. int main(void)
  39. {
  40. printf("\nANSWER(\"12FF\"): %d\n", HexStringToUInt("12FF"));
  41. printf("\nANSWER(\"abcd\"): %d\n", HexStringToUInt("abcd"));
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
ANSWER("12FF"): 4863

ANSWER("abcd"): 43981