Convert integer into its character equivalent, where 0 => a, 1 => b, etc

前端 未结 12 794
无人共我
无人共我 2020-11-28 02:52

I want to convert an integer into its character equivalent based on the alphabet. For example:

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

etc.

相关标签:
12条回答
  • 2020-11-28 03:20

    Assuming you want uppercase case letters:

    function numberToLetter(num){
            var alf={
                '0': 'A', '1': 'B', '2': 'C', '3': 'D', '4': 'E', '5': 'F', '6': 'G'
            };
            if(num.length== 1) return alf[num] || ' ';
            return num.split('').map(numberToLetter);
        }
    

    Example:

    numberToLetter('023') is ["A", "C", "D"]

    numberToLetter('5') is "F"

    0 讨论(0)
  • 2020-11-28 03:24

    If you don't mind getting multi-character strings back, you can support arbitrary positive indices:

    function idOf(i) {
        return (i >= 26 ? idOf((i / 26 >> 0) - 1) : '') +  'abcdefghijklmnopqrstuvwxyz'[i % 26 >> 0];
    }
    
    idOf(0) // a
    idOf(1) // b
    idOf(25) // z
    idOf(26) // aa
    idOf(27) // ab
    idOf(701) // zz
    idOf(702) // aaa
    idOf(703) // aab
    

    (Not thoroughly tested for precision errors :)

    0 讨论(0)
  • 2020-11-28 03:26

    There you go: (a-zA-Z)

    function codeToChar( number ) {
      if ( number >= 0 && number <= 25 ) // a-z
        number = number + 97;
      else if ( number >= 26 && number <= 51 ) // A-Z
        number = number + (65-26);
      else
        return false; // range error
      return String.fromCharCode( number );
    }
    

    input: 0-51, or it will return false (range error);

    OR:

    var codeToChar = function() {
      var abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
      return function( code ) {
        return abc[code];
      };
    })();
    

    returns undefined in case of range error. NOTE: the array will be created only once and because of closure it will be available for the the new codeToChar function. I guess it's even faster then the first method (it's just a lookup basically).

    0 讨论(0)
  • 2020-11-28 03:27

    If you are looking for TypeScript working functions then follow

    public numericValue = (alphaChar: any) => alphaChar.toUpperCase().charCodeAt(0) - 64;
    
    public alphaValue = (numericDigit: any) => 
       String.fromCharCode(64 + numericDigit) : '';
    

    You can make several checks like (numericDigit >= 1 && numericDigit <= 26) ? inside function body as per the requirements.

    0 讨论(0)
  • 2020-11-28 03:33

    Use String.fromCharCode. This returns a string from a Unicode value, which matches the first 128 characters of ASCII.

    var a = String.fromCharCode(97);
    
    0 讨论(0)
  • 2020-11-28 03:33

    The only problemo with @mikemaccana's great solution is that it uses the binary >> operator which is costly, performance-wise. I suggest this modification to his great work as a slight improvement that your colleagues can perhaps read more easily.

    const getColumnName = (i) => {
         const previousLetters = (i >= 26 ? getColumnName(Math.floor(i / 26) -1 ) : '');
         const lastLetter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[i % 26]; 
         return previousLetters + lastLetter;
    }
    

    Or as a one-liner

    const getColumnName = i => (i >= 26 ? getColumnName(Math.floor(i / 26) -1 ) : '') + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[i % 26];
    

    Example:

    getColumnName(0); // "A"
    getColumnName(1); // "B"
    getColumnName(25); // "Z"
    getColumnName(26); // "AA"
    getColumnName(27); // "AB"
    getColumnName(80085) // "DNLF"
    
    0 讨论(0)
提交回复
热议问题