使用求和方程JAVASCRIPT将它们转换为数字的单词块

Block of words converting them into number using sum equation JAVASCRIPT

本文关键字:转换 数字 单词块 求和 方程 JAVASCRIPT      更新时间:2023-09-26

这是我在这个网站上关于javascript代码的第一个问题。我得到了一个RSA加密代码,我们的教授给了我们一种非常巧妙的加密文本的方法。

用户在文本字段中输入纯文本,然后程序应该能够:

第一个-每隔4个字符中断文本并将其存储到数组中(完成!)

问题 现在是我面临的。。。Second-使用radix= 16.(ex: Num("abc")=1*16^2+2*16^1+3*16^0))计算每个块的数值。这些字母应转换为匹配的数字,如

space= 0
A,a =1
B, b =2
C , c = 3
. 
. Z, z= 26

然后发送到进行计算的函数!:-(

然后程序继续使用加密函数对每个块进行加密等等…但是,目前,我正在寻找一种方法来首先解决这个谜团。。

我找了很多,但一无所获!!请指导我怎么做。非常感谢!!!

这是我的javascript代码。。

    function start()
    {
        var eB = document.getElementById( "encryptButton" );
        eB.addEventListener("click", str2num, false);
    }
function split(){
    var str = document.getElementById( "inputField" ).value;
    var chunks = [];
    for (var i = 0, charsLength = str.length; i < charsLength; i += 3) {
    chunks.push(str.substring(i, i + 3));
    }
   document.getElementById("results").innerHTML=chunks;
}
window.addEventListener("load", start, false);

这是HTML格式(用户输入文本的表单)。。

<html>
   <head>
      <meta charset = "utf-8">
      <title>RSA Algorithm</title>
      <script src = "rsaj.js"></script>
   </head>
   <body>
      <form action = "#">
         <p>Enter a sentence to encrypt:</p>
         <p><input id = "inputField" type = "text">
            <input id = "encryptButton" type = "button" value = "Encrypt"></p>      
         <div id = "results"> </div>
      </form>
   </body>
</html>

好吧,Javascript中的parseInt函数将基数参数作为第二个参数,可以根据给定的基数将字符串转换为数字

所以你可以做

var convert=parseInt("f", 16); //16
var convert2=parseInt("af", 16); //175

我不明白的是你用"z"作为例子。也许我不明白,但基数16是HEX码,因此存在以下符号;012345689abcdef

您的问题发生了一些变化。

我有以下内容:

  • 使用"加密"密钥创建映射
  • 听取输入字段的更改
  • 将输入值大写
  • 遍历每个字符的值
  • 搜索地图并获得它的索引(默认值=0,空格也是)
  • 根据搜索增加总值
  • 每次迭代都会递减功率
  • 将结果返回到div.results

jsfiddle:http://jsfiddle.net/kychan/X8R2p/4/

var map     = ' 123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result  = document.getElementById('result');
function calculate(e)
{
    //    lower the cases.
    var x = e.value.toUpperCase();
    //    var holding total value.
    var total = 0;
    //    decrement on each character.
    //    start with the length of entered value -1.
    var decrementor = e.value.length-1;
    //    iterate through the string.
    for (var i in x)
    {
        //    search in map.
        var r = map.indexOf(x[i]);
        //    0 = 0, so no need to sum up or store it in the map.
        //    space = 0, in the map because 1 needs to return 1.
        if (r>-1)
        {
            total     +=    r*Math.pow(16, decrementor);
            decrementor--;
        }
    }
    //    print results.
    result.innerHTML = total;
}