@@ -11,9 +11,8 @@ The equivalent Octal Number : 11
11
11
At this moment I can binary -> decimal -> octal. There probably is a better way to do this:
12
12
Convert every 3 binary digits (start from bit 0) to 1 octal digit, with binary table from 0 to 7
13
13
Ex: 00001100010001 = 00 001 100 010 001 = 1 4 2 1 = 1421
14
- 2. Input a dinamic array of chars
14
+ + 2. Input a dinamic array of chars
15
15
3. Convertion:
16
- Enum of binary valies from 0 to 7
17
16
Iterate over an array, assign each pair of three from the end of the array octal value
18
17
Output the value
19
18
4. Invalid input
@@ -33,22 +32,41 @@ enum is_valid{
33
32
int input_binary (char * pointer_to_binary_array );
34
33
void print_invalid_input ();
35
34
void print_binary_array (char * pointer_to_binary_array );
35
+ void convert_to_octal (char * pointer_to_octal_array , char * pointer_to_binary_array );
36
36
37
37
int main () {
38
- char * pointer ;
39
- pointer = (char * )malloc (1 * sizeof (char ));
40
- if (pointer == NULL ) {
38
+ char * pointer_binary ;
39
+ pointer_binary = (char * )malloc (1 * sizeof (char ));
40
+ if (pointer_binary == NULL ) {
41
41
printf ("Memory could not be allocated" );
42
42
} else {
43
- int is_valid = input_binary (pointer );
44
- print_binary_array (pointer );
43
+ int is_valid = input_binary (pointer_binary );
44
+ // print_binary_array(pointer_binary );
45
45
if (is_valid == FALSE) {
46
46
print_invalid_input ();
47
+ } else {
48
+ char * pointer_octal ;
49
+ pointer_octal = (char * )malloc (1 * sizeof (char ));
50
+ convert_to_octal (pointer_octal , pointer_binary );
47
51
}
48
52
}
49
53
return 0 ;
50
54
}
51
55
56
+ void convert_to_octal (char * pointer_to_octal_array , char * pointer_to_binary_array ) {
57
+ int array_lenght = 1 , one_step = 3 , index = 0 , biggest_binary = 8 , temp_octal = 0 , binary_divisor = 2 ;
58
+ char current_element = pointer_to_binary_array [index ];
59
+ while (current_element != '\0' ) {
60
+ for (int i = 0 ; i <= 3 ; ++ i ) {
61
+ if (current_element == 1 ) {
62
+ temp_octal += biggest_binary / binary_divisor ;
63
+
64
+ }
65
+ }
66
+ }
67
+
68
+ }
69
+
52
70
void print_binary_array (char * pointer_to_binary_array ) {
53
71
for (int i = 0 ; ;++ i ) {
54
72
if (pointer_to_binary_array [i ] == '\0' ) {
0 commit comments