如何用Javascript将字母转换为数字?

时间:2014-03-25 02:27:55

标签: javascript

如何在JavaScript中将字母转换为相应的数字?

例如:

a = 0
b = 1
c = 2
d = 3

我在converting numbers to letters beyond the 26 character alphabet上发现了这个问题,但它要求相反。

有没有办法在没有大阵列的情况下做到这一点?

8 个答案:

答案 0 :(得分:72)

您可以使用String.prototype.charCodeAt从字符串中的任何索引获取代码点*。如果您的字符串是单个字符,那么您需要索引0,而a的代码是97(很容易从JavaScript获得'a'.charCodeAt(0)),所以您可以这样做:< / p>

s.charCodeAt(0) - 97

如果你想反过来,String.fromCharCode接受Unicode代码点*并返回一个字符串。

String.fromCharCode(97 + n)

* not quite

答案 1 :(得分:11)

对不起,我以为是走另一条路。

请改为尝试:

var str = "A";
var n = str.charCodeAt(0) - 65;

答案 2 :(得分:6)

如果您希望像AA那样处理Excel之类的字母,那么您可以使用以下this question改编的函数:

function convertLetterToNumber(str) {
  var out = 0, len = str.length;
  for (pos = 0; pos < len; pos++) {
    out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
  }
  return out;
}

Stack Snippets中的演示:

&#13;
&#13;
function convertLetterToNumber(str) {
  var out = 0, len = str.length;
  for (pos = 0; pos < len; pos++) {
    out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
  }
  return out;
}

var testCase = ["A","B","C","Z","AA","AB","BY"];

var converted = testCase.map(function(obj) {
  return {
    letter: obj,
    number: convertLetterToNumber(obj)
  };

});

console.table(converted);
&#13;
&#13;
&#13;

答案 3 :(得分:4)

您可以将1分配给&#39; a&#39;而不是0, 使标点符号,数字或大写字母更容易跳过(或包含)。

function a1(txt, literal){
    if(!literal) txt= txt.toLowerCase();
    return txt.split('').map(function(c){
        return 'abcdefghijklmnopqrstuvwxyz'.indexOf(c)+1 || (literal? c: '');
    }).join(' ');
}

var str =&#39;再次问好,世界!&#39;;

/*  a1(str,'literal')>>value: (String)
H 5 12 12 15   1 7 1 9 14 ,   23 15 18 12 4 !
*/


/*  a1(str) >>value: (String)
8 5 12 12 15  1 7 1 9 14   23 15 18 12 4 
*/

答案 4 :(得分:4)

这很简短,支持大写和小写。

const alphaVal = (s) => s.toLowerCase().charCodeAt(0) - 97 + 1

答案 5 :(得分:3)

您可以创建一个映射值的对象:

function letterValue(str){
    var anum={
        a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, 
        l: 12, m: 13, n: 14,o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, 
        u: 21, v: 22, w: 23, x: 24, y: 25, z: 26
    }
    if(str.length== 1) return anum[str] || ' ';
    return str.split('').map(letterValue);
}

letterValue('zoo')返回:(数组)[26,15,15];

letterValue('z')返回:(数字)26

答案 6 :(得分:0)

您可以使用此数值计算器 - 它会为所有字符串(非区分大小写)添加所有字母值:

function getSum(total,num) {return total + num};
function val(yazı,harf,değer){ rgxp = new RegExp(değer,'gim'); text = yazı.toLowerCase();
if (text.indexOf(harf) > -1){ sonuc = text.split(harf).join(değer).match(rgxp).map(Number).reduce(getSum) }else{ sonuc=0 };
return sonuc;}

String.prototype.abjad = function() {
a = val(this,'a','1'); b = val(this,'b','2'); c = val(this,'c','3'); ç = val(this,'ç','4'); d = val(this,'d','5');
e = val(this,'e','6'); f = val(this,'f','7'); g = val(this,'g','8'); ğ = val(this,'ğ','9'); h = val(this,'h','10');
ı = val(this,'ı','11'); i = val(this,'i','12'); j = val(this,'j','13'); k = val(this,'k','14'); l = val(this,'l','15');
m = val(this,'m','16'); n = val(this,'n','17'); o = val(this,'o','18'); ö = val(this,'ö','19'); p = val(this,'p','20');
r = val(this,'r','21'); s = val(this,'s','22'); ş = val(this,'ş','23'); t = val(this,'t','24'); u = val(this,'u','25');
ü = val(this,'ü','26'); v = val(this,'v','27'); y = val(this,'y','28'); z = val(this,'z','29');
return a+b+c+ç+d+e+f+g+ğ+h+ı+i+j+k+l+m+n+o+ö+p+r+s+ş+t+u+ü+v+y+z
};

Ask();
function Ask() {
    text = prompt("Please enter your text");
    if (text != null) {
    alert("Gematrical value is: " + text.abjad())
    document.getElementById("result").innerHTML = text.abjad();
    }
}
Gematrical value is: <p id="result"></p>

https://jsfiddle.net/ZarifinKolesi/0xydecze/1

答案 7 :(得分:0)

我用过:

let a = 'dev';
let encode = '';
for (let i = 0; i < a.length; i++) {
  let x = a.slice(i, i+1);
  encode += x.charCodeAt(0);
  
}
console.log(encode);

结果返回:100101118