In the midst of developing a website, I am working on a feature that allows users to input a minimum of 256 characters/strings (with code verification), choose between ASCII or EBCDIC conversion, and view the converted text string displayed on the page based on their selection. The code is set up to display the user input (checking for a minimum of 256 characters and ensuring a radio button is selected before clicking the Run! button). Below are the functions explained with comments:
encoding.js:
function myFunction() {
//Get both elements
const ascii = document.getElementById('ascii')
const ebcdic = document.getElementById('ebcdic')
let str = document.getElementById("text_id");
let a = "ASCII Code is == > ";
// Checking if user enters more than 255 chars
if (str.value.length < 256) {
console.log("null");
return null;
// signifies alert if less than 256 characters
}
// Ensuring a radio button is selected
let radio_selected = false;
document.querySelectorAll('input[type="radio"]').forEach(function (radio) {
if (radio.checked) {
radio_selected = true;
}
})
if (!radio_selected) {
console.log("The radio has not been checked, please select a button");
return;
}
//Triggers condition based on which element is checked
if (ascii.checked) {
for (let i = 0; i < str.value.length; i++) {
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
}
}
else if (ebcdic.checked) {
for (let i = 0; i < str.value.length; i++) {
//Code to convert text to EBCDIC, need help with this
}
}
}
1.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Converter for ASCII or EBCDIC</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script src="encoding.js" defer></script>
</head>
<body style="text-align:center;">
<label for="text">Enter a text that is at least 256 characters long</label><br>
<input type="text" id="text_id" name="text" minlength="256">
<p>Select the following:</p>
<div>
<input id="ascii" type="radio" name="encoding" value="ascii">
<label for="ascii">ASCII</label>
</div>
<div>
<input id="ebcdic" type="radio" name="encoding" value="ebcdic">
<label for="ebcdic">EBCDIC</label>
</div>
<button onclick="myFunction()" type="button">"Run!"</button>
<label for="button">"Run!"</label>
<p id="demo" style="color:red;">
</body>
</html>
I have been researching approaches for converting text/string to EBCDIC on the site but haven't found any references beyond EBCDIC to ASCII, reverse conversions, and single character conversions. Full text/string to EBCDIC conversion seems to be missing.