Javascript 将数字转换为字母

Javascript 将数字转换为字母,javascript,numbers,alphabetical,Javascript,Numbers,Alphabetical,我想把一个数字转换成它对应的字母。例如: 1 = A 2 = B 3 = C 这可以在javascript中完成,而无需手动创建数组吗? 在php中,有一个range()函数可以自动创建数组。javascript中是否有类似的内容?是的,并进行了调整 var值=10; write((值+9).toString(36.toUpperCase())您可以使用String.fromCharCode(code)函数在不使用数组的情况下执行此操作,因为字母有连续的代码。例如:String.fromCh

我想把一个数字转换成它对应的字母。例如:

1 = A
2 = B
3 = C
这可以在javascript中完成,而无需手动创建数组吗? 在php中,有一个range()函数可以自动创建数组。javascript中是否有类似的内容?

是的,并进行了调整

var值=10;

write((值+9).toString(36.toUpperCase())您可以使用
String.fromCharCode(code)
函数在不使用数组的情况下执行此操作,因为字母有连续的代码。例如:
String.fromCharCode(1+64)
为您提供“A”,而
String.fromCharCode(2+64)
为您提供“B”,等等。

这可以帮助您

static readonly string[] Columns_Lettre = new[] { "A", "B", "C"};

public static string IndexToColumn(int index)
    {
        if (index <= 0)
            throw new IndexOutOfRangeException("index must be a positive number");

        if (index < 4)
            return Columns_Lettre[index - 1];
        else
            return index.ToString();
    }
static readonly string[]Columns\u Lettre=new[]{“A”、“B”、“C”};
公共静态字符串IndexToColumn(int索引)
{

下面的if(index代码段将字母表中的字符转换为数字系统

1=A
2=B

26=Z
27=AA
28=AB

78=BZ
79=CA
80=CB


我创建此函数是为了在打印时保存字符,但由于我不想处理可能最终形成的不正确单词,因此不得不放弃它。我构建了以下解决方案,以增强@esantos的回答

第一个函数定义了一个有效的查找编码字典。在这里,我使用了英语字母表中的所有26个字母,但下面的函数也同样适用:
“ABCDEFG”
“abcdefghijklmnopqrstuvwxyz012456789”
“GFEDCBA”
。使用其中一个字典将导致将基数为10的数字转换为基数
字典。长度
数字具有适当编码的数字。唯一的限制是字典中的每个字符必须是唯一的

function getDictionary() {
    return validateDictionary("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

    function validateDictionary(dictionary) {
        for (let i = 0; i < dictionary.length; i++) {
            if(dictionary.indexOf(dictionary[i]) !== dictionary.lastIndexOf(dictionary[i])) {
                console.log('Error: The dictionary in use has at least one repeating symbol:', dictionary[i])
                return undefined
            }
        }
        return dictionary
    }
}
更新

您还可以使用此函数获取现有的短名称格式,并将其返回到基于索引的格式

function shortNameToIndex(shortName) {
    //Takes the short name (e.g. F6, AA47) and converts to base indecies ({6, 6}, {27, 47})

    if (shortName.length < 2) {return undefined}    //Must be at least one letter and one number
    if (!isNaN(shortName.slice(0, 1))) {return undefined}  //If first character isn't a letter, it's incorrectly formatted

    let letterPart = ''
    let numberPart= ''
    let splitComplete = false
    let index = 1

    do {
        const character = shortName.slice(index - 1, index)
        if (!isNaN(character)) {splitComplete = true}
        if (splitComplete && isNaN(character)) {
            //More letters existed after the numbers. Invalid formatting.
            return undefined    
        } else if (splitComplete && !isNaN(character)) {
            //Number part
            numberPart = numberPart.concat(character)
        } else {
            //Letter part
            letterPart = letterPart.concat(character)
        }
        index++
    } while (index <= shortName.length)

    numberPart = parseInt(numberPart)
    letterPart = encodedLetterToNumber(letterPart)

    return {xIndex: numberPart, yIndex: letterPart}
}
函数shortNameToIndex(shortName){
//采用短名称(例如F6,AA47)并转换为基索引({6,6},{27,47})
if(shortName.length<2){return undefined}//必须至少是一个字母和一个数字
如果(!isNaN(shortName.slice(0,1)){return undefined}//如果第一个字符不是字母,则格式不正确
让letterPart=''
设numberPart=''
设splitComplete=false
设指数=1
做{
const character=shortName.slice(索引-1,索引)
如果(!isNaN(character)){splitComplete=true}
if(拆分完成(&isNaN)(字符)){
//数字后有更多字母。格式无效。
返回未定义
}else if(splitComplete&&!isNaN(字符)){
//数字部分
numberPart=numberPart.concat(字符)
}否则{
//字母部分
letterPart=letterPart.concat(字符)
}
索引++

}当(索引您可以共享示例输入和输出吗?@Fr0zenFyr:检查字母对应的ASCII值,如果数字>26,则会得到错误的结果。在这种情况下,您需要进行检查并制定规则。@Fr0zenFyr最简单的方法是使用模:
var value=36%26;
//结果也是o
J
e如果您想添加一些关于代码的解释,我会这样使用它:
String.fromCharCode(1+'A'.charCodeAt(0))
function numberToEncodedLetter(number) {
    //Takes any number and converts it into a base (dictionary length) letter combo. 0 corresponds to an empty string.
    //It converts any numerical entry into a positive integer.
    if (isNaN(number)) {return undefined}
    number = Math.abs(Math.floor(number))

    const dictionary = getDictionary()
    let index = number % dictionary.length
    let quotient = number / dictionary.length
    let result
    
    if (number <= dictionary.length) {return numToLetter(number)}  //Number is within single digit bounds of our encoding letter alphabet

    if (quotient >= 1) {
        //This number was bigger than our dictionary, recursively perform this function until we're done
        if (index === 0) {quotient--}   //Accounts for the edge case of the last letter in the dictionary string
        result = numberToEncodedLetter(quotient)
    }

    if (index === 0) {index = dictionary.length}   //Accounts for the edge case of the final letter; avoids getting an empty string
    
    return result + numToLetter(index)

    function numToLetter(number) {
        //Takes a letter between 0 and max letter length and returns the corresponding letter
        if (number > dictionary.length || number < 0) {return undefined}
        if (number === 0) {
            return ''
        } else {
            return dictionary.slice(number - 1, number)
        }
    }
}
function encodedLetterToNumber(encoded) {
    //Takes any number encoded with the provided encode dictionary 

    const dictionary = getDictionary()
    let result = 0
    let index = 0

    for (let i = 1; i <= encoded.length; i++) {
        index = dictionary.search(encoded.slice(i - 1, i)) + 1
        if (index === 0) {return undefined} //Attempted to find a letter that wasn't encoded in the dictionary
        result = result + index * Math.pow(dictionary.length, (encoded.length - i))
    }

    return result
}
console.log(numberToEncodedLetter(4))     //D
console.log(numberToEncodedLetter(52))    //AZ
console.log(encodedLetterToNumber("BZ"))  //78
console.log(encodedLetterToNumber("AAC")) //705
function shortNameToIndex(shortName) {
    //Takes the short name (e.g. F6, AA47) and converts to base indecies ({6, 6}, {27, 47})

    if (shortName.length < 2) {return undefined}    //Must be at least one letter and one number
    if (!isNaN(shortName.slice(0, 1))) {return undefined}  //If first character isn't a letter, it's incorrectly formatted

    let letterPart = ''
    let numberPart= ''
    let splitComplete = false
    let index = 1

    do {
        const character = shortName.slice(index - 1, index)
        if (!isNaN(character)) {splitComplete = true}
        if (splitComplete && isNaN(character)) {
            //More letters existed after the numbers. Invalid formatting.
            return undefined    
        } else if (splitComplete && !isNaN(character)) {
            //Number part
            numberPart = numberPart.concat(character)
        } else {
            //Letter part
            letterPart = letterPart.concat(character)
        }
        index++
    } while (index <= shortName.length)

    numberPart = parseInt(numberPart)
    letterPart = encodedLetterToNumber(letterPart)

    return {xIndex: numberPart, yIndex: letterPart}
}