这里只是将 0-26 位转为对应的 A-Z,主要用于 索引转为选项 ,用于在线考试,问卷调查等场景
/*
* 数字序号转字符串序号 0 => "A"
*/
function indexToString(index){
var charcode;
return index.toString(26).split("").map(function(item,i){
charcode = item.charCodeAt(0);
charcode+=(charcode>=97?10:49);
return String.fromCharCode(charcode)
}).join("").toUpperCase();
}
/*
* 字符串序号转数字序号 "A" => 0
*/
function stringToIndex(str){
var charcode;
return parseInt(str.toLowerCase().split("").map(function(item,i){
charcode = item.charCodeAt(0);
charcode-=(charcode<107?49:10);
return String.fromCharCode(charcode)
}).join(""),26);
}
indexToString(984284) => "CEABC"
stringToIndex("CEABC") => 984284