acobzew

sem3-problem1

Apr 8th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 100
  4.  
  5. int fromHexToDec(char s[]) {
  6.     int ans = 0, i = 0, pow = 1;
  7.  
  8.     for (i = strlen(s) - 1; i >= 0; i--) {
  9.         if (s[i] >= '0' && s[i] <= '9')
  10.             ans += (s[i] - '0') * pow;
  11.         else if (s[i] >= 'a' && s[i] <= 'f')
  12.             ans += (s[i] - 'a' + 10) * pow;
  13.         else if (s[i] >= 'A' && s[i] <= 'F')
  14.             ans += (s[i] - 'A' + 10) * pow;
  15.        
  16.         pow *= 16;
  17.     }
  18.  
  19.     return ans;
  20. }
  21.  
  22. int main() {
  23.     char s[N];
  24.    
  25.     scanf("%s", s);
  26.     printf("%d", fromHexToDec(s));
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment