-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
78 lines (71 loc) · 1.17 KB
/
tools.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
#include <stdarg.h>
#include <stdlib.h>
#include "base.h"
#include "reduc.h"
void convert_bin_to_octal(char* dec)
{
char* bin;
int count;
int calc;
bin = malloc(sizeof(*bin) * 3);
calc = 0;
count = 7;
while (count >= 0)
{
if(dec[count] == '1')
calc = bin_octal(count, calc);
if (count == 5 || count == 2 || count == 0)
{
bin = bin_octal_s(count, bin, calc);
calc = 0;
}
count = count - 1;
}
id_print_str(bin);
free(bin);
}
void convert_bin_to_hexa(char* dec, char c)
{
char* bin;
int count;
int calc;
bin = malloc(sizeof(*bin) * 2);
if (c == 'x')
c = 'a';
else
c = 'A';
count = 0;
calc = 0;
while (count != 8)
{
if(dec[count] == '1')
calc = bin_hexa(count, calc);
if (count == 3 || count == 7)
{
bin = bin_hexa_s(count, calc, bin, c);
calc = 0;
}
count = count + 1;
}
id_print_str(bin);
free(bin);
}
void convert_dec_to_bin(int dec, char c)
{
char bin[8];
int count;
int calc1;
count = 7;
calc1 = 0;
while (count >= 0)
{
calc1 = dec % 2;
dec = dec / 2;
bin[count] = '0' + calc1;
count = count - 1;
}
if (c == 'x' || c == 'X')
convert_bin_to_hexa(bin,c);
else if (c == 'o')
convert_bin_to_octal(bin);
}