-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertor.c
More file actions
102 lines (78 loc) · 1.72 KB
/
convertor.c
File metadata and controls
102 lines (78 loc) · 1.72 KB
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
/*
Criado por relue271
Convertor de bases
*/
#include <stdio.h>
#include <string.h>
void main(void){
int decimal;
printf("\nDigite um numero inteiro: ");
scanf("%d", &decimal);
//converte o decimal para binario
int aux, i, resto;
aux = decimal;
i=0;
char binario[1024] = "";
do{
resto = aux % 2;
aux = aux / 2;
binario[i] = resto+'0';
i++;
}while(aux > 0);
binario[i] = '\0';
printf("\nB: ");
for (i=strlen(binario); i>=0; i--) {
if ((i % 4) == 0) { printf(" ");}
printf("%c", binario[i]);
}
printf(" .");
//converte o decimal para hexadecimal
aux = decimal;
i=0;
char hexa[256] = "";
do {
resto = aux % 16;
aux = aux / 16;
switch (resto) {
case 10:
hexa[i] = 'A';
break;
case 11:
hexa[i] = 'B';
break;
case 12:
hexa[i] = 'C';
break;
case 13:
hexa[i] = 'D';
break;
case 14:
hexa[i] = 'E';
break;
case 15:
hexa[i] = 'F';
break;
default:
hexa[i] = resto+'0';
}
i++;
}while(aux > 0);
hexa[i] = '\0';
printf("\nH: ");
for (i=strlen(hexa); i>=0; i--) { printf("%c", hexa[i]);}
printf(" .");
//converte decimal para octal
aux = decimal;
i=0;
char octal[256] = "";
do{
resto = aux % 8;
aux = aux / 8;
octal[i] = resto+'0';
i++;
}while(aux > 0);
octal[i] = '\0';
printf("\nO: ");
for (i=strlen(octal); i>=0; i--) { printf("%c", octal[i]); }
printf(" .\n\n");
}