-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.c
74 lines (64 loc) · 1.45 KB
/
converter.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
#include "includes/converter.h"
char* dec2bin(long int c, int len) {
int i;
char* ret = (char*)malloc(len+1);
strcpy(ret, "");
for(i = len-1; i >= 0; i--){
if((c & (1 << i)) != 0){
strcat(ret, "1");
}
else{
strcat(ret, "0");
}
}
return ret;
}
int bin2dec(char *bin, int len){
int exp = 1, number = 0;
for(int i = len - 1; i >= 0; i--) {
if(bin[i] == '1')
number += exp;
exp *= 2;
}
return number;
}
char* xor(char* check, char* next8bits) {
int i;
char* ret = (char*)malloc(8+1);
strcpy(ret, "");
for(i = 0; i < 8; i++){
if(check[i] == next8bits[i]){
strcat(ret, "0");
}
else{
strcat(ret, "1");
}
}
return ret;
}
char* and(char* check, char* next8bits) {
int i;
char* ret = (char*)malloc(8);
char aca = '0';
strcpy(ret, "");
for(i = 7; i >= 0; i--){
if(check[i] == next8bits[i]) {
if(aca == '1')
strcat(ret, "1");
else
strcat(ret, "0");
if(check[i] == '1')
aca = '1';
else
aca = '0';
}
else{
if(aca == '1')
strcat(ret, "0");
else
strcat(ret, "1");
}
}
printf("%s ^ %s = %s\n", check, next8bits, ret);
return ret;
}