-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainJs.js
137 lines (118 loc) · 3.52 KB
/
mainJs.js
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
//*************************************************************************** */
// BDHO Converter
// Number system converter
// design by Amit barman on 14/06/2022
//follow me on github - https://github.com/AmitBarman99/
//*************************************************************************** */
//conversion
function convert(){
var iType=document.getElementById("inpType").value;
var iNum=document.getElementById("inpNum").value;
var oType=document.getElementById("opType").value;
var oNum;
//all the conditions
//decimal to binary,octal and hexa-decimal
if(iType=="inpdec" && oType=="opbin"){
oNum=dec_to_bin(iNum);
}
if(iType=="inpdec" && oType=="opoct"){
oNum=dec_to_oct(iNum);
}
if(iType=="inpdec" && oType=="ophex"){
oNum=dec_to_hex(iNum);
}
if(iType=="inpdec" && oType=="opdec"){
oNum="Invalid";
}
//binary to decimal,octal and hexa-decimal
if(iType=="inpbin" && oType=="opdec"){
oNum=bin_to_dec(iNum);
}
if(iType=="inpbin" && oType=="opoct"){
oNum=bin_to_oct(iNum);
}
if(iType=="inpbin" && oType=="ophex"){
oNum=bin_to_hex(iNum);
}
if(iType=="inpbin" && oType=="opbin"){
oNum="Invalid";
}
//octal to decimal, binary and hexa-decimal
if(iType=="inpoct" && oType=="opdec"){
oNum=oct_to_dec(iNum);
}
if(iType=="inpoct" && oType=="opbin"){
oNum=oct_to_bin(iNum);
}
if(iType=="inpoct" && oType=="ophex"){
oNum=oct_to_hex(iNum);
}
if(iType=="inpoct" && oType=="opoct"){
oNum="Invalid";
}
//hexa-decimal to decimal,binary and octal
if(iType=="inphex" && oType=="opdec"){
oNum=hex_to_dec(iNum);
}
if(iType=="inphex" && oType=="opbin"){
oNum=hex_to_bin(iNum);
}
if(iType=="inphex" && oType=="opoct"){
oNum=hex_to_oct(iNum);
}
if(iType=="inphex" && oType=="ophex"){
oNum="Invalid";
}
document.getElementById("opNum").value = oNum;
// preventDefault();
return false;
}
// function for decimal to binary conversion
function dec_to_bin(iNum){
// return parseInt(iNum).toString(2);
return parseFloat(iNum).toString(2);
}
// function for decimal to octal conversion
function dec_to_oct(iNum){
return parseFloat(iNum).toString(8);
}
// function for decimal to hexa-decimal conversion
function dec_to_hex(iNum){
return parseFloat(iNum).toString(16);
}
// function for binary to decimal conversion
function bin_to_dec(iNum){
return parseInt(iNum,2).toString(10);
}
// function for binary to octal conversion
function bin_to_oct(iNum){
return parseInt(iNum,2).toString(8);
}
// function for binary to hexa-decimal conversion
function bin_to_hex(iNum){
return parseInt(iNum,2).toString(16);
}
// function for octal to decimal conversion
function oct_to_dec(iNum){
return parseInt(iNum,8).toString(10);
}
// function for octal to binary conversion
function oct_to_bin(iNum){
return parseInt(iNum,8).toString(2);
}
// function for octal to hexa-decimal conversion
function oct_to_hex(iNum){
return parseInt(iNum,8).toString(16);
}
// function for hexa-decimal to decimal conversion
function hex_to_dec(iNum){
return parseInt(iNum,16).toString(10);
}
// function for hexa-decimal to binary conversion
function hex_to_bin(iNum){
return parseInt(iNum,16).toString(2);
}
// function for hexa-decimal to octal conversion
function hex_to_oct(iNum){
return parseInt(iNum,16).toString(8);
}