|
| 1 | +/****************************************************************************** |
| 2 | +* * |
| 3 | +* File: hex2dec.c * |
| 4 | +* Author: Alison Chaiken <alchaiken@gmail.com> * |
| 5 | +* Created: Wed Jul 14 18:40:04 PDT 2010 * |
| 6 | +* Contents: Convert hex-formatted numbers to decimal format and * |
| 7 | +* and print the results. Accepts input on command-line or from * |
| 8 | +* stdin. * |
| 9 | +* * |
| 10 | +* Distributed under version 3 of the GNU General Public License, * |
| 11 | +* see http://www.gnu.org/licenses/gpl.html for more info. * |
| 12 | +* * |
| 13 | +******************************************************************************/ |
| 14 | + |
| 15 | +#include <math.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdio.h> |
| 18 | +#include <string.h> |
| 19 | +#include <ctype.h> |
| 20 | + |
| 21 | +#define MAXSTRING 100 |
| 22 | + |
| 23 | +void usage(int bad_digit){ |
| 24 | + printf("hex2dec: Illegal digit %c: use A-F, a-f and 0-9 with \ |
| 25 | + leading 'Ox'.\n", bad_digit); |
| 26 | +} |
| 27 | + |
| 28 | +void process_token (char *str_token) |
| 29 | +{ |
| 30 | + int i, digits, startdigit, coeff, thisdigit; |
| 31 | + long unsigned outnum = 0; |
| 32 | + |
| 33 | + digits = strlen(str_token); |
| 34 | + if ((str_token[0] == 48) && (str_token[1] == 120)) |
| 35 | + startdigit = 2; /* skip over 0x prefix */ |
| 36 | + else if ((str_token[0] >= 48) && (str_token[1] <= 57)) |
| 37 | + startdigit = 0; |
| 38 | + else usage(str_token[0]); |
| 39 | + for (i = startdigit; i < digits; i++) { |
| 40 | + thisdigit = str_token[i]; |
| 41 | + |
| 42 | + /* in ASCII, zero is 48, nine is 57 */ |
| 43 | + if ((thisdigit >= 48) && (thisdigit <= 57)) |
| 44 | + coeff = thisdigit - 48; |
| 45 | + /* A is 65, F is 70 */ |
| 46 | + else if ((thisdigit >= 65) && (thisdigit <= 70)) |
| 47 | + coeff = thisdigit - 55; |
| 48 | + /* a is 97, f is 102 */ |
| 49 | + else if ((thisdigit >= 97) && (thisdigit <= 102)) |
| 50 | + coeff = thisdigit - 87; |
| 51 | + else { |
| 52 | + usage(thisdigit); |
| 53 | + exit(1); |
| 54 | + } |
| 55 | + outnum += coeff * (long unsigned) pow(16, (digits - i - 1)); |
| 56 | + } |
| 57 | + printf("%lu ", outnum); |
| 58 | + return; |
| 59 | +} |
| 60 | + |
| 61 | +void process_line(char *thisline) |
| 62 | +{ |
| 63 | + char *next_token; |
| 64 | + |
| 65 | + next_token = strtok(thisline," "); |
| 66 | + do { |
| 67 | + process_token(next_token); |
| 68 | + } while ((next_token = strtok(NULL," ")) != NULL) ; |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +int main(int argc, char *argv[]) |
| 73 | +{ |
| 74 | + |
| 75 | + int i; |
| 76 | + |
| 77 | + char instring[MAXSTRING], *stringp; |
| 78 | + |
| 79 | + if (argc > 1) { /* args to be converted on command line */ |
| 80 | + strcpy(instring,argv[1]); |
| 81 | + for (i=2;i<argc;i++){ |
| 82 | + strcat(instring," "); |
| 83 | + strcat(instring,argv[i]); |
| 84 | + } |
| 85 | + process_line(instring); |
| 86 | + } |
| 87 | + |
| 88 | + else { |
| 89 | + /* args from stdin */ |
| 90 | + while (fgets(instring,MAXSTRING,stdin) != NULL){ |
| 91 | + stringp = instring; |
| 92 | + /* line from stdin ends in '\n' */ |
| 93 | + /* while ((*stringp++ = getchar())!= '\n') */ |
| 94 | + while (*stringp++ != '\n') |
| 95 | + ; |
| 96 | + /* make last character a null instead of '\n' */ |
| 97 | + *--stringp='\0'; |
| 98 | + process_line(instring); |
| 99 | + } |
| 100 | + } |
| 101 | + printf("\n"); |
| 102 | + |
| 103 | + exit(0); |
| 104 | +} |
0 commit comments