-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2oct.c
103 lines (82 loc) · 2.39 KB
/
bin2oct.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
/* Converts binary to hexadecimal */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define BINARY_GROUP_LENGTH 3 /* groups of 3 bits, for octal conversion */
typedef struct String{
char* data;
int len;
} String;
String btoo(const char *s);
int bin2oct(char *in);
int main(int argc, char **argv)
{
String s;
int i;
if(argc != 2) {
printf("Converts character string from binary to octal notation\n"
"Usage: ./bin2oct STRING\n"
"Example ./bin2oct 1001\n"
" ./bin2oct 0b1001\n"
" ./bin2oct 0B1001\n"
);
return -1;
}
s = btoo(argv[1]);
printf("Binary: %s\nOctal: %s\n", argv[1], s);
free(s.data);
return 0;
}
/* converts a string of binary digits to octal */
String btoo(const char *s)
{
int i, k, j;
int prev_in_len ,in_len, out_len;
char *temp; /* temp char array used for input string processing */
String result_str; /* output string */
in_len = strlen(s);
/* accounts for prefix */
if (s[0] == '0' && (s[1] == 'b' || s[1] == 'B')){
in_len -= 2;
i = 2;
} else i = 0;
/* set up grouping conversion algorithm by making the number of bits a multiple of 3 */
prev_in_len = in_len;
while(in_len % BINARY_GROUP_LENGTH != 0) in_len++;
/* allocate memory and copy input bit string */
temp = malloc(in_len * sizeof(char));
strcpy(temp + in_len - prev_in_len, s);
for (j = 0; j < in_len - prev_in_len; ++j)
temp[j] = '0';
/* allocate memory for output string*/
out_len = in_len / BINARY_GROUP_LENGTH;
result_str.data = malloc(out_len * sizeof(char));
/* iterate over input string and get octal representation for each group of 3 bits */
k = 0;
while (i < in_len) {
result_str.data[k] = bin2oct(temp+i) + '0';
i += 3;
++k;
}
result_str.data[out_len] = '\0'; /* add null termination character */
result_str.len = out_len;
free(temp);
return result_str;
}
/* return octal value from groups of binary strings of length 3 */
int bin2oct(char *in)
{
int i;
int power;
int result;
power = BINARY_GROUP_LENGTH - 1;
result = 0;
for (i = 0; i < BINARY_GROUP_LENGTH; ++i){
if(in[i] - '0')
result += pow(2,power);
--power;
}
return result;
}