-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberConversion
209 lines (192 loc) · 6.84 KB
/
NumberConversion
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
public class NumberConversion {
public static int binaryTodecimal(int bin_num,int deci_num,int index) {
//Termination case
if(bin_num==0){
return deci_num;
}
//Business Logic
deci_num = deci_num + (int)(Math.pow(2,index)*(bin_num%10));
//Recursive call
return binaryTodecimal(bin_num/10,deci_num,index+1);
}
public static void decimalTobinary(int deci_num,String bin_num) {
//Termination case
if(deci_num==0){
System.out.println("Binary value of decinal number is: "+ bin_num);
return;
}
//Business logic
bin_num = deci_num%2+bin_num;
//Recursive call
decimalTobinary(deci_num/2, bin_num);
}
public static void binaryToctal(int deci_num,String oct_num,int index) {
//Termination case
if(deci_num==0){
System.out.println("Octal value of binary number is:"+ oct_num);
return;
}
//Business Logic
oct_num = deci_num%8 + oct_num;
//Recursive call
binaryToctal(deci_num/10,oct_num,index+1);
}
public static void octalTobinary(int oct_num,String bin_num) {
//Termination case
if(oct_num==0){
System.out.println("Binary value of octal number is: "+ bin_num);
return;
}
//Business logic
switch(oct_num%10){
case 0: bin_num = "000"+bin_num;
break;
case 1: bin_num = "001"+bin_num;
break;
case 2: bin_num = "010"+bin_num;
break;
case 3: bin_num = "011"+bin_num;
break;
case 4: bin_num = "100"+bin_num;
break;
case 5: bin_num = "101"+bin_num;
break;
case 6: bin_num = "110"+bin_num;
break;
case 7: bin_num = "111"+bin_num;
break;
case 8: bin_num = "001"+bin_num;
break;
default:System.out.println("Not working!!");
}
//Recursive call
octalTobinary(oct_num/10, bin_num);
}
public static int octalTodecimal(int octal_num,int deci_num,int index) {
//Termination case
if(octal_num==0){
return deci_num;
}
//Business Logic
deci_num = deci_num + (int)(Math.pow(8,index)*(octal_num%10));
//Recursive call
return octalTodecimal(octal_num/10,deci_num,index+1);
}
public static void HexaTobinary(String hexa_num,String bin_num) {
//Termination case
if(hexa_num.length()==0){
System.out.println("Binary value of Hexadecimal number is: "+ bin_num);
return;
}
//Business logic
switch(hexa_num.charAt(0)){
case '0': bin_num = bin_num + "0000";
break;
case '1': bin_num = bin_num + "0001";
break;
case '2': bin_num = bin_num + "0010";
break;
case '3': bin_num = bin_num + "0011";
break;
case '4': bin_num = bin_num + "0100";
break;
case '5': bin_num = bin_num + "0101";
break;
case '6': bin_num = bin_num + "0110";
break;
case '7': bin_num = bin_num + "0111";
break;
case '8': bin_num = bin_num + "1000";
break;
case '9': bin_num = bin_num + "1001";
break;
case 'A': bin_num = bin_num + "1010";
break;
case 'B': bin_num = bin_num + "1011";
break;
case 'C': bin_num = bin_num + "1100";
break;
case 'D': bin_num = bin_num + "1101";
break;
case 'E': bin_num = bin_num + "1110";
break;
case 'F': bin_num = bin_num + "1111";
break;
default:System.out.println("Not working!!");
}
//Recursive call
HexaTobinary(hexa_num.substring(1), bin_num);
}
public static int HexaTodecimal(String hexa_num,int deci_num,int index,int count) {
//Termination case
if(index<0){
return deci_num;
}
//Business Logic
//System.out.println(hexa_num.charAt(index));
if(hexa_num.charAt(index)=='A'){
deci_num = deci_num + (int)(Math.pow(16,count)*(10));
}
else if(hexa_num.charAt(index)=='B'){
deci_num = deci_num + (int)(Math.pow(16,count)*(11));
}
else if(hexa_num.charAt(index)=='C'){
deci_num = deci_num + (int)(Math.pow(16,count)*(12));
}
else if(hexa_num.charAt(index)=='D'){
deci_num = deci_num + (int)(Math.pow(16,count)*(13));
}
else if(hexa_num.charAt(index)=='E'){
deci_num = deci_num + (int)(Math.pow(16,count)*(14));
}
else if(hexa_num.charAt(index)=='F'){
deci_num = deci_num + (int)(Math.pow(16,count)*(15));
}
else{
deci_num = deci_num + (int)(Math.pow(16,count)*(Integer.parseInt(""+hexa_num.charAt(index))));
}
//Recursive call
return HexaTodecimal(hexa_num,deci_num,index-1,count+1);
}
public static void decimalToHexa(int deci_num,String hexa_num) {
//Termination case
if(deci_num==0){
System.out.println("Hexadecimal value of decinal number is: "+ hexa_num);
return;
}
//Business logic
if(deci_num%16==10){
hexa_num = "A"+hexa_num;
}
else if(deci_num%16==11){
hexa_num = "B"+hexa_num;
}
else if(deci_num%16==12){
hexa_num = "C"+hexa_num;
}
else if(deci_num%16==13){
hexa_num = "D"+hexa_num;
}
else if(deci_num%16==14){
hexa_num = "E"+hexa_num;
}
else if(deci_num%16==15){
hexa_num = "F"+hexa_num;
}
else{
hexa_num = deci_num%16+hexa_num;
}
//Recursive call
decimalToHexa(deci_num/16, hexa_num);
}
public static void main(String[] args) {
System.out.println("The decimal value of binary number is: "+binaryTodecimal(10101, 0, 0));
decimalTobinary(21, "");
binaryToctal(binaryTodecimal(10101, 0, 0), "", 0);
octalTobinary(21, "");
System.out.println("The decimal value of octal number is: "+octalTodecimal(25, 0, 0));
HexaTobinary("2A", "");
System.out.println("The decimal value of Hexadecimal number is: "+ HexaTodecimal("6A", 0, ("6A".length()-1),0));
decimalToHexa(125, "");
}
}