-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
153 lines (137 loc) · 3.94 KB
/
index.html
File metadata and controls
153 lines (137 loc) · 3.94 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Binary ↔ Decimal Converter</title>
<style>
/* Minimal dark-mode styling */
body {
margin: 0;
padding: 1rem;
background: #121212;
color: #fff;
font-family: sans-serif;
display: flex;
flex-direction: column;
gap: 1.5rem;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
}
h1 {
margin: 0;
font-size: 1.5rem;
text-align: center;
}
.converter {
display: flex;
flex-direction: column;
gap: 0.5rem;
width: 100%;
max-width: 500px;
}
label {
font-weight: bold;
margin-bottom: 0.2rem;
}
input {
width: 100%;
padding: 0.6rem;
border: none;
border-radius: 4px;
background: #222;
color: #fff;
font-size: 1rem;
}
button {
padding: 0.6rem 1rem;
border: none;
border-radius: 4px;
background: #333;
color: #fff;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background: #444;
}
.output {
background: #1e1e1e;
margin-top: 0.3rem;
}
@media (max-width: 600px) {
h1 {
font-size: 1.2rem;
}
}
</style>
</head>
<body>
<h1>Binary ↔ Decimal Converter</h1>
<div class="converter">
<label for="decimalInput">Decimal Input</label>
<input type="text" id="decimalInput" placeholder="Enter decimal" />
<button onclick="convertDecimalToBinary()">Convert Decimal → Binary</button>
<input type="text" id="decimalToBinaryOutput" class="output" placeholder="Binary result" readonly />
</div>
<div class="converter">
<label for="binaryInput">Binary Input</label>
<input type="text" id="binaryInput" placeholder="Enter binary" />
<button onclick="convertBinaryToDecimal()">Convert Binary → Decimal</button>
<input type="text" id="binaryToDecimalOutput" class="output" placeholder="Decimal result" readonly />
</div>
<script>
function convertDecimalToBinary() {
const decimalInput = document.getElementById('decimalInput').value.trim();
const outputField = document.getElementById('decimalToBinaryOutput');
if (!decimalInput) {
outputField.value = '';
return;
}
// Split by '.' to handle multiple segments (like IP address segments)
const segments = decimalInput.split('.');
try {
const binarySegments = segments.map(seg => {
const num = parseInt(seg, 10);
if (isNaN(num) || num < 0 || num > 255) {
throw new Error('Invalid decimal segment');
}
// Convert to binary and left-pad to 8 bits
return num.toString(2).padStart(8, '0');
});
// Rejoin with '.'
outputField.value = binarySegments.join('.');
} catch (err) {
outputField.value = 'Invalid input';
}
}
function convertBinaryToDecimal() {
const binaryInput = document.getElementById('binaryInput').value.trim();
const outputField = document.getElementById('binaryToDecimalOutput');
if (!binaryInput) {
outputField.value = '';
return;
}
// Split by '.' for multiple segments
const segments = binaryInput.split('.');
try {
const decimalSegments = segments.map(seg => {
// parse binary (up to 8 bits if IP style)
if (seg.length > 8) {
throw new Error('Binary segment longer than 8 bits');
}
const num = parseInt(seg, 2);
if (isNaN(num) || num < 0 || num > 255) {
throw new Error('Invalid binary segment');
}
return num.toString();
});
outputField.value = decimalSegments.join('.');
} catch (err) {
outputField.value = 'Invalid input';
}
}
</script>
</body>
</html>