数字与字母相互转换

数字序号转字符串序号  0 => "A"
export 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
export 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);
}

 
posted @ 2021-04-25 17:42  赵小胖丶  阅读(288)  评论(0)    收藏  举报