fork(1) download
  1. #include <stdio.h>
  2. int c16toi10( char c );
  3. int main( void )
  4. {
  5. int n;
  6. char str[20];
  7. char *p;
  8. printf("正の16進数の入力 ==>");
  9. scanf("%s", str);
  10.  
  11. n = 0;
  12. p = str;
  13. if( c16toi10( *p ) != -1 )
  14. n += c16toi10( *p++ );
  15. while( c16toi10( *p ) != -1 )
  16. {
  17. n <<= 4;
  18. n += c16toi10( *p++ );
  19. }
  20. if( *p == '\0' )
  21. printf("%s(16) = %d(10)\n", str, n);
  22. else
  23. printf("ERROR\n");
  24. return 0;
  25. }
  26.  
  27. int c16toi10( char c )
  28. {
  29. int rtn;
  30. if('0' <= c && c <= '9')
  31. rtn = c - '0';
  32. else if('A' <= c && c <= 'F')
  33. rtn = c - 0x37;
  34. else if('a' <= c && c <= 'f')
  35. rtn = c - 0x57;
  36. else
  37. rtn = -1;
  38. return rtn;
  39. }
Success #stdin #stdout 0.01s 1724KB
stdin
Standard input is empty
stdout
正の16進数の入力 ==>ERROR