-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvesion.c
115 lines (106 loc) · 2.47 KB
/
convesion.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void dec_oct(void);
void dec_hex(void);
void reverse(char *str, int, int);
int main()
{
int choice;
printf("\t--------------\n");
printf("\tRadix converter\n\t----------------\n");
while (1)
{ // uncomment for repetitive conversion
printf("Select conversion type\n");
printf("1.Decimal to octal\n2.Decimal to hexadecimal\n3.Exit\n");
scanf("%d", &choice);
if (choice == 1)
dec_oct();
else if (choice == 2)
dec_hex();
else if (choice == 3)
return 0;
else
{
printf("Invalid choice \n");
printf("Try again!! \n");
}
}
return 0;
}
void dec_oct()
{
int dec, dec_h;
int i = 0;
char oct_val[10];
bool end_conv = false;
printf("\n Enter the decimal number (interger) \n");
scanf("%d", &dec);
dec_h = dec;
while (!end_conv)
{
int mod;
if (dec < 8)
{
oct_val[i] = dec + 48; // 48 comes from ascii table
// printf("%c",oct_val[i]);
end_conv = true;
}
else
{
mod = dec % 8;
dec = (dec - mod) / 8;
oct_val[i] = mod + 48;
}
i++;
}
oct_val[i] = '\0';
reverse(oct_val, 0, strlen(oct_val) - 1);
printf("%d in octal is: %s\n\n", dec_h, oct_val);
}
void dec_hex()
{
int dec, dec_h, i = 0;
bool end_conv = false;
char hex_val[10];
printf("\n Enter the decimal number (interger) \n");
scanf("%d", &dec);
dec_h = dec;
while (!end_conv)
{
int mod;
if (dec < 16)
{
if (dec > 9)
hex_val[i] = dec + 55; // 55 comes from ascii table
else
hex_val[i] = dec + 48;
end_conv = true;
}
else
{
mod = dec % 8;
dec = (dec - mod) / 16;
if (mod > 9)
hex_val[i] = mod + 55;
else
hex_val[i] = mod + 48;
}
i++;
}
hex_val[i] = '\0';
reverse(hex_val, 0, strlen(hex_val) - 1);
printf("%d in hex is: %s\n\n", dec_h, hex_val);
}
// reversing string by recursion
void reverse(char *str, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(str + begin);
*(str + begin) = *(str + end);
*(str + end) = c;
reverse(str, ++begin, --end);
}