-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitToBinary.c
146 lines (114 loc) · 3.48 KB
/
DigitToBinary.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
136
137
138
139
140
141
142
143
144
145
146
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct {
int bits;
int *value;
} Binary;
Binary digitToBinary(int digit) {
Binary binary;
// To calculate the number of bits to represent x in binary required use this formula :
// (log2 (x)) + 1
binary.bits = log2(digit) + 1;
// Formula :
// https://indepth.dev/posts/1019/the-simple-math-behind-decimal-binary-conversion-algorithms#converting-decimal-integer-to-binary
binary.value = (int*)calloc(binary.bits, sizeof(int));
if (binary.value == NULL) {
printf("Memory allocation failed\n");
binary.bits = 0;
}
int i = binary.bits - 1; // Weird work around, because turns out the array
// size is n + 1
while (digit > 0) {
binary.value[i] = digit % 2;
digit = digit / 2;
i--;
}
return binary;
}
void destroyArr(Binary *binary) {
if (binary -> value != 0) {
free(binary -> value);
binary -> bits = 0;
}
}
int binaryToDigit(char binaryInputChar[]) {
int arrLength = strlen(binaryInputChar);
int binaryInputInt[arrLength];
int digitResult = 0;
for (int i = 0; i < arrLength; i++) {
binaryInputInt[i] = (int)(binaryInputChar[i]) - 48; // Output Ascii, subtract with 48
// to get the actual value
// Formula :
// https://indepth.dev/posts/1019/the-simple-math-behind-decimal-binary-conversion-algorithms#converting-binary-integer-to-decimal
digitResult = 2 * digitResult + binaryInputInt[i];
}
return digitResult;
}
int main(int argc, char *argv[]) {
char *optionsList[] = {"--DigitToBinary", "--BinaryToDigit"};
int optListArrLen = sizeof(optionsList) / sizeof(optionsList[0]);
if (argc < 2) {
printf(
"Please pass Options + Argument \n"
"\nAvailable options : \n"
"%s : For Converting Digits to Binary\n"
"%s : For Converting Binary to Digits\n"
"\nArgument = Digit or Binary that want to be convert\n"
"\nExample : \n"
"./<ThisProgramName> %s 123\n",
optionsList[0], optionsList[1], optionsList[0]
);
return 0;
}
int selectedOpt;
for (selectedOpt = 0; selectedOpt < optListArrLen + 1; selectedOpt++) {
if (strcmp(argv[1], optionsList[selectedOpt]) == 0) {
break;
}
}
if (selectedOpt > optListArrLen) {
printf("Cannot use '%s' as options\n", argv[1]);
return 0;
}
if (argc == 2) {
printf("Expected some argument\n");
return 0;
}
int argLen = strlen(argv[2]);
int inputInt = atoi(argv[2]);
if (inputInt == 0 && argLen > 1) {
printf("Expected a number as argument \n");
return 0;
}
if (inputInt == 0) {
printf("%d\n", inputInt);
return 0;
}
if (argc > 3) {
printf("Only the first argument the will be converted \n");
}
if (selectedOpt == 0) {
Binary binary = digitToBinary(inputInt);
if (binary.bits != 0) {
for (int i = 0; i < binary.bits; i++) { // Eventhough the array size is n+1
printf("%d", binary.value[i]); // But this behaviour is similar to array size
} // equal to n ?
printf("\n");
return 0;
}
destroyArr(&binary);
}
for (int i = 0; i < argLen; i++) {
inputInt = (int)argv[2][i] - 48;
if (inputInt > 1 || inputInt < 0) {
printf("'%s' is not binary\n", argv[2]);
return 0;
}
}
if (selectedOpt == 1) {
printf("%d\n", binaryToDigit(argv[2]));
}
}