Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define MAXLEN 1000
- /* convert hex to decimal */
- main()
- {
- char input[MAXLEN];
- printf("Hex number: 0x");
- printf("Decimal conversion: %d\n", htoi(input));
- return 0;
- }
- int htoi(char s[])
- {
- int i, c, num, dec, res;
- res = 0;
- for (i=0; i<MAXLEN-1 && (c=getchar())!=EOF && c!='\n'; ++i)
- s[i] = c;
- num = i;
- ++i;
- s[i] = '\0';
- i = 0;
- for (num; num > 0; --num) {
- if (s[i] >= 'A' && s[i] <= 'F')
- dec = toupper(s[i]) - 'A' + 10;
- else if (s[i] >= '0' && s[i] <= '9')
- dec = s[i] - '0';
- else
- dec = 0;
- res = res + (double) pow(16, (num-1)) * dec;
- ++i;
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement