I am currently trying to explore how to convert user-supplied plaintext to hexadecimal characters in ASCII and EBCDIC. The website I’m developing requires the user to supply a string (256 chars min), select ASCII or EBCDIC radio buttons, and convert the user-supplied string to the selected encoding choice in hexadecimal values.
Currently, I have been able to convert user-supplied strings into ASCII in hexadecimal characters.
However, I find difficulty in being able to convert that same string into EBCDIC hexadecimal characters due to its limited usage. Below is the code I use for both ASCII and EBCDIC:
if (ascii.checked) {
for (let i = 0; i < str.value.length; i++)
{
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "0" + str.value.charCodeAt(i).toString(16);
//document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
//console.log(toString(16));
}
}
else if (ebcdic.checked) {
for (let i = 0; i < str.value.length; i++)
{
//loop to check all values entered
//Code to convert text to EBCDIC, need help with this
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
}
}
Here is the full code which has encoding.js, style.css, and 1.html:
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 == > ";
// Below checks to see if the user selects writed more than 255 chars
if (str.value.length < 256) {
console.log("null");
return null;
// prints and returns null if the user entered a string less than 256 characters
}
// Below checks to see if the user selects a radio button
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;
}
//If one of the elements is checked it triggers a condition, if the other is cheked it triggers the other condition
if (ascii.checked) {
for (let i = 0; i < str.value.length; i++)
{
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "0" + str.value.charCodeAt(i).toString(16);
//document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
//console.log(toString(16));
}
}
else if (ebcdic.checked) {
for (let i = 0; i < str.value.length; i++)
{
//loop to check all values entered
//Code to convert text to EBCDIC, need help with this
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
}
}
}
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>
style.css:
body
{
font: 12pt Arial;
}
input[type=radio] + label::before
{
border-radius: 10px;
}
input {
border: 2px solid currentcolor;
}
input:invalid {
border: 2px dashed red;
}
input:invalid:focus {
background-image: linear-gradient(pink, lightgreen);
}
encod