-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex2bin.c
135 lines (114 loc) · 3.35 KB
/
hex2bin.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* converts hex to binary */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BINARY_GROUP_LENGTH 4 /* each hex char is represented by four binary chars*/
/* struct defining a simple string */
typedef struct String{
char* data;
int len;
} String;
String htob(const char *s);
void make_binary(int c, char *res);
void put_binary(String destination, char *source, int *dest_index);
int main(int argc, char **argv)
{
String s;
int i;
if(argc != 2) {
printf("Converts character string from hexadecimal to binary notation\n"
"Usage: ./hex2bin STRING\n"
"Example ./hex2bin 16AB\n"
" ./hex2bin 0x16AB\n"
" ./hex2bin 0X16AB\n"
);
return -1;
}
s = htob(argv[1]);
printf("Hex: %s\nBinary: %s\n", argv[1], s);
free(s.data);
return 0;
}
/* converts a string of hexadecimal digits to binary*/
String htob(const char *s)
{
int i, k;
int output_array_len, input_array_len;
char bits[BINARY_GROUP_LENGTH];
String result_str;
input_array_len = strlen(s);
/* accounts for prefix */
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')){
input_array_len -= 2;
i = 2;
} else i = 0;
output_array_len = BINARY_GROUP_LENGTH * input_array_len;
result_str.data = malloc(output_array_len * sizeof(char));
k = 0;
while (i < input_array_len) {
if(isdigit(s[i])) {
make_binary(s[i] - '0', bits);
put_binary(result_str, bits, &k);
} else {
switch(s[i]) {
case 'A':
case 'a':
make_binary(10, bits);
put_binary(result_str, bits, &k);
break;
case 'B':
case 'b':
make_binary(11, bits);
put_binary(result_str, bits, &k);
break;
case 'C':
case 'c':
make_binary(12, bits);
put_binary(result_str, bits, &k);
break;
case 'D':
case 'd':
make_binary(13, bits);
put_binary(result_str, bits, &k);
break;
case 'E':
case 'e':
make_binary(14, bits);
put_binary(result_str, bits, &k);
break;
case 'F':
case 'f':
make_binary(15, bits);
put_binary(result_str, bits, &k);
break;
}
}
++i;
}
result_str.data[output_array_len] = '\0'; /* add null termination character */
result_str.len = output_array_len;
return result_str;
}
/* make binary output string from hex character */
void make_binary(int c, char* res)
{
int i, j;
i = 0;
do {
res[i] = c % 2 + '0';
c /= 2;
++i;
} while (c != 0);
while(i < BINARY_GROUP_LENGTH){
res[i] = '0';
++i;
}
}
/* copy source bits into destination, updating destination index */
void put_binary(String destination, char *source, int *dest_index)
{
int i, j;
for(j = BINARY_GROUP_LENGTH - 1; j >= 0; --j)
destination.data[(*dest_index)++] = source[j];
}