Skip to content

Commit a87c6b2

Browse files
committed
Working with binary to octal conversion
1 parent 1c44a9b commit a87c6b2

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

For loop/53. Binary number to octal.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ The equivalent Octal Number : 11
1111
At this moment I can binary -> decimal -> octal. There probably is a better way to do this:
1212
Convert every 3 binary digits (start from bit 0) to 1 octal digit, with binary table from 0 to 7
1313
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
1515
3. Convertion:
16-
Enum of binary valies from 0 to 7
1716
Iterate over an array, assign each pair of three from the end of the array octal value
1817
Output the value
1918
4. Invalid input
@@ -33,22 +32,41 @@ enum is_valid{
3332
int input_binary(char *pointer_to_binary_array);
3433
void print_invalid_input();
3534
void print_binary_array(char *pointer_to_binary_array);
35+
void convert_to_octal(char *pointer_to_octal_array, char *pointer_to_binary_array);
3636

3737
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) {
4141
printf("Memory could not be allocated");
4242
} 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);
4545
if (is_valid == FALSE) {
4646
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);
4751
}
4852
}
4953
return 0;
5054
}
5155

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+
5270
void print_binary_array(char *pointer_to_binary_array) {
5371
for (int i = 0; ;++i) {
5472
if (pointer_to_binary_array[i] == '\0') {

0 commit comments

Comments
 (0)