-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoct2bin.c
128 lines (104 loc) · 2.62 KB
/
oct2bin.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/* length for octal prefix*/
#define OCTAL_PREFIX_NEW 2 /* refers to octal prefix of form "0o" like "0x" for hex */
#define OCTAL_PREFIX_OLD 1 /* refers to octal prefix of form "0" as in 0177 */
typedef struct String {
char* data;
int len;
} String;
int otoi(const char *s);
String dtob(int decimal);
void insert_binary_char(char *s, int num, int pos);
int main(int argc, char **argv)
{
int i;
int decimal;
String s_bin;
if(argc != 2) {
printf("Converts character string from octal to binary notation\n"
"Usage: ./oct2bin STRING\n"
"Example ./oct2bin 0100\n"
"Example ./oct2bin 0o100\n"
"Example ./oct2bin 0O100\n"
);
return -1;
}
decimal = otoi(argv[1]);
s_bin = dtob(decimal);
printf("Octal: %s\nBinary: ", argv[1]);
for (i = s_bin.len; i >= 0; --i)
putchar(s_bin.data[i]);
putchar('\n');
free(s_bin.data);
return 0;
}
/* converts a string of decimal digits to binary */
String dtob(int decimal)
{
int i;
int copy;
char *result;
int remainder;
String str;
copy = decimal;
/* count number of divisions */
i = 0;
while (copy / 2 != 0) {
++i;
copy /= 2;
}
++i; /* null termination character */
/* allocate memory for result array */
result = malloc(i * sizeof(char));
result[i] = '\0'; /* add null termination character */
/* convert decimal to binary by division */
i = 0;
while(decimal / 2 != 0) {
insert_binary_char(result, decimal, i);
decimal /= 2;
++i;
}
insert_binary_char(result, decimal, i);
str.data = result;
str.len = i;
return str;
}
/* inserts char in array based on value of num */
void insert_binary_char(char *s, int num, int pos)
{
int remainder;
remainder = num % 2;
s[pos] = remainder + '0';
}
/* converts a string of octal digits to decimal*/
int otoi(const char *s)
{
int i;
int len, result, power;
len = strlen(s);
/* if 0o or 0O is present, or 0, update the start index */
if (s[0] == '0' && (s[1] == 'o' || s[1] == 'O')) {
i = 2;
power = len - OCTAL_PREFIX_NEW - 1;
} else if (s[0] == '0') {
i = 1;
power = len - OCTAL_PREFIX_OLD - 1;
}
else {
i = 0;
power = len - 1;
}
result = 0;
while (i < len) {
if(isdigit(s[i])) {
result += (s[i] - '0') * pow(8, power);
--power;
}
++i;
}
return result;
}