如何在 JS 中将字符串/文本转换为 EBCDIC?

问题描述

我正在编写一个网站,该网站接受至少 256 个字符/字符串的用户输入(针对该要求进行代码检查),提示(也检查)用户的转换选择(用户选择 ASCII 或 EBCDIC),并将转换后的文本字符串输出到页面上(根据用户的喜好以 ASCII 或 EBCDIC 格式)。该代码能够打印出用户输入(检查最少输入 256 个字符并确保用户在按下 Run! 按钮之前选择了一个单选按钮)。下面的代码有以下注释解释功能:

编码.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>

我一直在浏览此站点以查找任何文本/字符串(不是单个字符)转换为 EBCDIC 的迹象。到目前为止,我只看到了 EBCDIC 到 ASCII、反向和单个字符的转换,但没有看到字符串/文本到 EBCDIC 转换的迹象。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)