sierra117
sierra117

Reputation: 39

How to convert strings/text to EBCDIC in JS?

I am in the process of writing a website that accepts user input of at least 256 characters/strings (code checks for that requirement), prompts(also checks) the user's choice of conversion (user selects either ASCII or EBCDIC), and outputs the converted text string onto the page (in ASCII or EBCDIC based on user's preference). The code is capable of printing out the user input (checking for minimum input of 256 characters and making sure the user selects a radio button before pressing the Run! button). The code below has the following with comments explaining the functions:

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 + str.value.charCodeAt(i);
        }
    }
    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
        }
    }
}

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 browsing this site for any sign of text/string (not single characters) conversion to EBCDIC. So far, I've only seen EBCDIC to ASCII, the reverse, and individual character conversions but no sign of string/text to EBCDIC conversions.

Upvotes: 0

Views: 1645

Answers (1)

phunsoft
phunsoft

Reputation: 2745

I'm no javascript programmer, so I cannot help with the code, but I'll try to outline the steps you need to be able to write the code for the ASCII to EBCDIC conversion.

  • You need to build a 256 element translation table.
  • The index into the table is the value you get by calling charCodeAt() for each single character of the input field.
  • The element value at that index is the EBCDIC value for the corresponding ASCII character. As an example, let's take character "A". This has the ASCII value 65 (decimal), and the EBCDIC value 193 (decimal). So the entry at index 65 holds the value 193.
  • Use some special some value, e.g. 991, if the ASCII character does not have a representation in the EBCDIC table.
  • Do this for every (printable) ASCII character.
  • Once all the entries for (printable) ASCII characters have been filled, you need to fill the remaining entries. Again use some value, e.g. 992.
  • The loop uses the charCodeAt() value for each input character to index into the table. The value found is then either the value for the character in EBCDIC, or the indication for a non-translatable character (991), or for a non-printable character (992).

Hope this helps you to start with coding.

Upvotes: 2

Related Questions