INTRODUCTION
Binary is a number system that uses only two digits, typically represented by 0 and 1. This system is widely used in digital electronics and computer programming because it is easy to represent using electronic devices that can be either "on" or "off", which correspond to the binary digits of 1 and 0.In binary, each digit (or bit) represents a power of 2, with the rightmost bit representing 2^0 (or 1), the next bit representing 2^1 (or 2), and so on. To represent a number in binary, you can add up the values of the bits that are set to 1. For example, the binary number 1011 represents the decimal number 11, because 1 x 2^3 + 0 x 2^2 + 1 x 2^1 + 1 x 2^0 = 8 + 0 + 2 + 1 = 11.
Binary is also used to represent data in computers, where each character is represented by a unique combination of 1s and 0s. For example, the letter "A" might be represented by the binary sequence 01000001. This allows computers to store, transmit, and process data using electronic circuits that can easily distinguish between on and off states.
MAIN CONTENT<!DOCTYPE html>
<html>
<head>
<title>Binary Converter</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/2b0d9a2ad2.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
<script>
function binaryToSentence() {
// Get the input value
const input = document.getElementById("binary-input").value.trim();
// Split the binary input into an array of bytes
const bytes = input.match(/.{1,8}/g);
// Convert each byte to a character and join the characters to form the sentence
const sentence = bytes.map(byte => String.fromCharCode(parseInt(byte, 2))).join("");
// Display the result
const result = document.getElementById("result");
result.innerText = sentence;
result.style.color = "#fff";
result.style.backgroundColor = "#29b6f6";
result.style.animation = "fade-in 0.5s ease-out";
}
function sentenceToBinary() {
// Get the input value
const input = document.getElementById("sentence-input").value.trim();
// Convert each character to a binary string and join the strings to form the binary
const binary = input.split("").map(char => char.charCodeAt(0).toString(2).padStart(8, "0")).join("");
// Display the result
const result = document.getElementById("result");
result.innerText = binary;
result.style.color = "#fff";
result.style.backgroundColor = "#f44336";
result.style.animation = "fade-in 0.5s ease-out";
}
</script>
</head>
<body>
<div class="container">
<h1>Binary Converter</h1>
<div class="converter">
<div class="form">
<label for="binary-input">Enter binary:</label>
<input type="text" id="binary-input" placeholder="01000001 01100010 01100011" />
<button onclick="binaryToSentence()">Convert to sentence</button>
<label for="sentence-input">Enter sentence:</label>
<input type="text" id="sentence-input" placeholder="Abc" />
<button onclick="sentenceToBinary()">Convert to binary</button>
</div>
<div class="result">
<div id="result"></div>
</div>
</div>
</div>
</body>
</html>
Comments
Post a Comment