Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define N 100
- int fromHexToDec(char s[]) {
- int ans = 0, i = 0, pow = 1;
- for (i = strlen(s) - 1; i >= 0; i--) {
- if (s[i] >= '0' && s[i] <= '9')
- ans += (s[i] - '0') * pow;
- else if (s[i] >= 'a' && s[i] <= 'f')
- ans += (s[i] - 'a' + 10) * pow;
- else if (s[i] >= 'A' && s[i] <= 'F')
- ans += (s[i] - 'A' + 10) * pow;
- pow *= 16;
- }
- return ans;
- }
- int main() {
- char s[N];
- scanf("%s", s);
- printf("%d", fromHexToDec(s));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment