Binary to Text Convert Using JavaScript with HTML

Hello Friends Today, through this tutorial, I will tell you How to Binary to Text Convert Using Javascript with HTML? You can create a simple HTML page with JavaScript to convert binary to text. Here’s an example:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Binary to Text Converter</title>
</head>
<body>
<h2>Binary to Text Converter</h2>
<label for="binaryInput">Enter Binary:</label>
<input type="text" id="binaryInput" placeholder="Enter binary here 01110110">
<button onclick="convertBinaryToText()">Convert</button>

<h3>Converted Text:</h3>
<p id="convertedText"></p>

<script>
function convertBinaryToText() {
const binaryInput = document.getElementById('binaryInput').value;
const convertedText = binaryToText(binaryInput);
document.getElementById('convertedText').textContent = convertedText;
}
function binaryToText(binary) {

// Ensure the input is a valid binary string
if (!/^[01]+$/.test(binary)) {
alert("Invalid binary input. Please enter a valid binary string.");
return '';
}

// Convert binary to ASCII
const chunks = binary.match(/.{1,8}/g);
const asciiChars = chunks.map(chunk => String.fromCharCode(parseInt(chunk, 2)));

// Join the ASCII characters to form the text
return asciiChars.join('');

}
</script>
</body>
</html>

Copy and paste this code into an HTML file and open it in a web browser. Enter a binary string in the input field, click the “Convert” button, and the corresponding text will be displayed below. This example uses a simple conversion where each group of 8 bits (1 byte) is converted to its ASCII equivalent character.