-
Notifications
You must be signed in to change notification settings - Fork 0
/
atob.c
94 lines (78 loc) · 1.47 KB
/
atob.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
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void atob(FILE *ifp, FILE * ofp){
fseek(ifp, 0L, SEEK_END);
unsigned size = ftell(ifp);
fseek(ifp, 0L, SEEK_SET);
char * input = (char *)malloc(size * sizeof(char));
fread(input, sizeof(char), size, ifp);
char * output = (char *)malloc(size * 8 * sizeof(char));
output[0] = '\0';
//convert
for (unsigned i = 0; i < size; i++){
char tmp[9];
tmp[0] = '\0';
if (input[i] > 127){
strcat(tmp, "1");
input[i] -= 128;
} else {
strcat(tmp, "0");
}
if (input[i] > 63){
strcat(tmp, "1");
input[i] -= 64;
} else {
strcat(tmp, "0");
}
if (input[i] > 31){
strcat(tmp, "1");
input[i] -= 32;
} else {
strcat(tmp, "0");
}
if (input[i] > 15){
strcat(tmp, "1");
input[i] -= 16;
} else {
strcat(tmp, "0");
}
if (input[i] > 7){
strcat(tmp, "1");
input[i] -= 8;
} else {
strcat(tmp, "0");
}
if (input[i] > 3){
strcat(tmp, "1");
input[i] -= 4;
} else {
strcat(tmp, "0");
}
if (input[i] > 1){
strcat(tmp, "1");
input[i] -= 2;
} else {
strcat(tmp, "0");
}
if (input[i] == 1){
strcat(tmp, "1");
} else {
strcat(tmp, "0");
}
strncat(output, tmp, 8);
}
fprintf(ofp, "%s", output);
}
int main(int argc, char ** argv){
if (argc != 3){
fprintf(stderr, "usage: ./atob <input> <output>\n");
return -1;
}
FILE *ifp = fopen(argv[1], "r");
FILE *ofp = fopen(argv[2], "w");
atob(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}