Skip to content

Commit e1317e9

Browse files
author
Alison Chaiken
committed
hex2dec dec2hex: two format converters that read from CLI or from stdin
Signed-off-by: Alison Chaiken <alison_chaiken@mentor.com>
0 parents  commit e1317e9

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

dec2hex.c

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/******************************************************************************
2+
* *
3+
* File: dec2hex.c *
4+
* Author: Alison Chaiken <alison@hildesheim> *
5+
* Created: Sat Mar 8 18:59:36 CET 2014 *
6+
* Contents: Convert decimal-formatted numbers to hex format and *
7+
* and print the results. Accepts input on command-line or from *
8+
* stdin. *
9+
* *
10+
* Copyright (c) 2014 Alison Chaiken. *
11+
* Distributed under version 3 of the GNU General Public License, *
12+
* see http://www.gnu.org/licenses/gpl.html for more info. *
13+
* *
14+
******************************************************************************/
15+
16+
17+
#include <math.h>
18+
#include <stdlib.h>
19+
#include <stdio.h>
20+
#include <string.h>
21+
#include <ctype.h>
22+
23+
#define MAXSTRING 100
24+
#define BASE 16
25+
26+
void usage(void){
27+
fprintf(stderr, "dec2hex: illegal input.\n");
28+
exit(-1);
29+
}
30+
31+
void process_token (char *str_token) {
32+
int digits;
33+
long long unsigned thistoken = 0, ratio, divisor, outnum = 0;
34+
35+
digits = strlen(str_token);
36+
if (!digits) return;
37+
38+
/* handle '0' input */
39+
if ((str_token[0] == '0') && (digits == 1))
40+
digits = 0;
41+
42+
if ((str_token[0] == '0') && ((str_token[1] == 'x') || (str_token[1]
43+
== 'X'))) {
44+
digits -= 2;
45+
str_token += 2;
46+
}
47+
if (digits && ((sscanf(str_token, "%llu", &thistoken) ==
48+
EOF) || (thistoken == 0)))
49+
usage();
50+
while (thistoken && digits) {
51+
divisor = (long long unsigned) pow(BASE, (digits-1));
52+
ratio = thistoken/divisor;
53+
outnum += (long long unsigned) ratio*pow(BASE, (digits - 1));
54+
thistoken -= (ratio*divisor);
55+
digits--;
56+
}
57+
switch (BASE) {
58+
case 16:
59+
printf("0x%llX\n", outnum);
60+
break;
61+
case 10:
62+
case 2:
63+
printf("%llu\n", outnum);
64+
break;
65+
case 8:
66+
printf("0%llo\n", outnum);
67+
break;
68+
default:
69+
printf("Unsupported format\n");
70+
exit(-1);
71+
}
72+
73+
return;
74+
}
75+
76+
77+
void process_line(char *thisline) {
78+
char *next_token;
79+
80+
next_token = strtok(thisline," ");
81+
do {
82+
process_token(next_token);
83+
} while ((next_token = strtok(NULL," ")) != NULL) ;
84+
}
85+
86+
int main(int argc, char *argv[]) {
87+
int i;
88+
char instring[MAXSTRING], *stringp;
89+
90+
if (argc > 1){ /* args to be converted on command line */
91+
strcpy(instring,argv[1]);
92+
for (i=2;i<argc;i++){
93+
strcat(instring," ");
94+
strcat(instring,argv[i]);
95+
}
96+
process_line(instring);
97+
98+
}
99+
100+
else {
101+
/* args from stdin */
102+
while (fgets(instring,MAXSTRING,stdin) != NULL) {
103+
stringp = instring;
104+
/* line from stdin ends in '\n' */
105+
/* while ((*stringp++ = getchar())!= '\n') */
106+
while (*stringp++ != '\n')
107+
;
108+
/* make last character a null instead of '\n' */
109+
*--stringp='\0';
110+
process_line(instring);
111+
}
112+
}
113+
114+
exit(0);
115+
116+
}

hex2dec.c

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)