-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexon.c
83 lines (66 loc) · 1.57 KB
/
hexon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# include <stdio.h>
# include <strings.h>
# include <stdlib.h>
//hexon
//prints hex values beneath text reads from files or from stdio
// Thomas Garside
void readFile(char*);
char hexNums(int);
void readStdin();
void processLine(char*);
int main(int argc, char** argv){
// read list of files from argv if none exist read from stdin
if (argc > 1){
for( int e = 0; e < argc; e++)
readFile(argv[e]);
}
else{
readStdin();
}
}
//read from stdin
void readStdin(){
char* line = NULL;
size_t len = 0;
ssize_t read;
while((read = getline(&line, &len, stdin)) != -1){
processLine(line);
}
}
//process and print a single line
void processLine(char* line){
int linelen=strlen(line);
char line1[linelen];
char line2[linelen];
for( int i=0; i<linelen; i++){
line1[i] = hexNums(line[i]);
line2[i] = hexNums(line[i]/16);;
}
printf(" %s", line);
printf("|%s|\n",line2);
printf("|%s|\n",line1);
}
// process a single file
void readFile(char* filename){
FILE* file = fopen(filename, "r");
char* line = NULL;
size_t len = 0;
ssize_t read;
if (file == NULL)
exit(EXIT_FAILURE);
while((read = getline(&line, &len, file)) != -1){
processLine(line);
}
}
// convert a char to its hex components
char hexNums(int charVal){
char hexnum;
int temp = charVal % 16;
if (temp < 10){
hexnum = temp + 48;
}
else {
hexnum = temp + 55;
}
return hexnum;
}