-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
162 lines (154 loc) · 6.04 KB
/
index.html
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
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
<title>Binary Translator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link id="theme" rel="stylesheet" type="text/css" href="light-theme.css" />
</head>
<body>
<style>
body {
padding: 55px;
background-color: white;
color: black;
font-size: 15px;
}
.dark-mode {
background-color: black;
color: white;
}
.button {
background-color: inherit;
border: none;
color: white;
padding: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 12px;
margin: 4px 2px;
cursor: pointer;
}
.button1 {
border-radius: 12px;
}
/* Green */
.button1 {
color: green;
}
.button1:hover {
background-color: #04AA6D;
color: white;
}
</style>
<button class="button button1" onclick="myFunction()">Toggle Dark mode</button>
<div class="container-fluid">
<header class="row border border-bottom-1">
<h1 class="col-12 col-sm-12 text-center text-dark">Binary Code Translator</h1>
</header>
<div class="row d-flex justify-content-center mt-3">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" value="" name="opcion" id="binary_text">
<label class="form-check-label" for="binary_text"> Binary to Text </label>
</div>
<div class="form-check form-check-inline">
<input type="radio" class="form-check-input" value="" name="opcion" id="text_binary" checked>
<label for="text_binary" class="form-check-label">Text to Binary</label>
</div>
</div>
<div class="row d-flex flex-column align-items-center mt-3">
<div class="input-group col-12 col-sm-6">
<input type="text" id="sentence" placeholder="Enter the sentence" class="form-control" aria-label="Enter the sentence" aria-describedby="btn_action" />
<button type="button" class="btn btn-primary" id="btn_action">Convert</button>
</div>
<div class="col-12 col-sm-6 mt-2 d-none" id="alert">
<div class="alert alert-warning" role="alert">
<strong>Warning!</strong> You have not put any value.
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-6 mx-auto mt-3 d-flex flex-column">
<div class="border text-center text-primary rounded p-3">
<span id="container-text"></span>
</div>
<button type="button" class="btn btn-info w-100 position-relative fixed-bottom" id="reverse" disabled>Convert to the Inverse</button>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script>
<script>
const $CONTAINER_TEXT = document.getElementById("container-text");
const $INPUT_TEXT = document.getElementById("sentence");
function binary_to_text(binary = "") {
let bin = binary.split(" ");
let unicodes = bin.map((e) => parseInt(e, 2));
let sentence = String.fromCharCode(...unicodes);
$CONTAINER_TEXT.innerHTML = sentence;
}
function text_to_binary(text = "") {
let characters = text.split("");
let binary;
let binary_text = "";
characters.forEach((char) => {
binary = char.charCodeAt().toString(2); //Convert from ASCII Code to BINARY Code
binary_text += `${binary} `;
});
$CONTAINER_TEXT.innerHTML = `
<p>${binary_text}</p>`;
}
document.getElementById("btn_action").addEventListener("click", (e) => {
let checked = document.getElementById("binary_text").checked;
if ($INPUT_TEXT.value == "") {
document.getElementById("alert").classList.remove("d-none");
setTimeout(() => {
document.getElementById("alert").classList.add("d-none");
}, 2500);
} else {
document.getElementById("reverse").disabled = false;
if (checked) {
binary_to_text($INPUT_TEXT.value);
} else {
text_to_binary($INPUT_TEXT.value);
}
$INPUT_TEXT.value = null;
}
});
document.getElementById("reverse").addEventListener("click", (e) => {
e.stopPropagation();
let value = document.getElementById("container-text").innerText;
let radios = document.getElementsByName("opcion");
let radio_checked, radio_unchecked;
radios.forEach(r => {
if (r.checked) {
radio_checked = r;
} else {
radio_unchecked = r;
}
});
switch (radio_checked.id) {
case "binary_text":
text_to_binary(value);
radio_unchecked.checked = true;
break;
case "text_binary":
binary_to_text(value);
radio_unchecked.checked = true;
break;
default:
console.error("An error has occured!");
break;
}
$INPUT_TEXT.value = value;
});
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
</script>
</body>
</html>