197

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. I could build an array and just look it up when I need it but I’m wondering if there’s a built in function to do this for me. All the examples I’ve found via Google are working with ASCII values and not a character’s position in the alphabet.

3

12 Answers 12

359

Assuming you want lower case letters:

var chr = String.fromCharCode(97 + n); // where n is 0, 1, 2 ...

97 is the ASCII code for lower case 'a'. If you want uppercase letters, replace 97 with 65 (uppercase 'A'). Note that if n > 25, you will get out of the range of letters.

0
88

Will be more portable in case of extending to other alphabets:

char='abcdefghijklmnopqrstuvwxyz'[code]

or, to be more compatible (with our beloved IE):

char='abcdefghijklmnopqrstuvwxyz'.charAt(code);
9
  • 6
    Much more elegant than String.fromCharCode in my opinion, as as you said, it extends very easily. Jun 29, 2010 at 22:01
  • 8
    And when you have no need of extending, maybe more prone to errors? abcede Jun 29, 2010 at 22:07
  • 5
    FYI JScript (IE) does not support the index operator [] on strings. Jun 29, 2010 at 22:21
  • 4
    @Crescent, the [] property accessor on strings is supported on IE from IE8 up (IE8 in IE7 compat mode also doesn't works), String.prototype.chatAt is preferred instead of [] for browser compatibility. E.g. 'foo'.charAt(0) == 'f' Jun 29, 2010 at 22:53
  • 2
    @Crescent, forgot to mention that the [] property accessor on strings is standardized on ECMAScript 5 (see [[GetOwnProperty]](P)). Jun 29, 2010 at 23:01
34

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]
  );
}

[0, 1, 25, 26, 27, 701, 702, 703].map(idOf);
// ['a', 'b', 'z', 'aa', 'ab', 'zz', 'aaa', 'aab']

(Not thoroughly tested for precision errors :)

5
  • 1
    Recursive function, very nice! Mar 22, 2016 at 22:24
  • @mikemaccana, why this edit? I think it makes it harder to read. Now I have to scroll horizontally to read the code.
    – z0r
    May 11, 2017 at 1:23
  • @z0r So people using the code won't have to fix the newline. There's no reason to break lines arbitrarily, editors will wrap at the character size of their window. May 12, 2017 at 13:42
  • 1
    Nice idea, but it didn't work until changed into this :function idOf(i) { return (i >= 26 ? idOf(Math.floor(i / 26) -1 ) : '') + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[i % 26]; }
    – PatrickvL
    Apr 14, 2020 at 15:02
  • exactly what I was looking or
    – jones
    Sep 2, 2021 at 5:53
20

A simple answer would be (26 characters):

String.fromCharCode(97+n);

If space is precious you could do the following (20 characters):

(10+n).toString(36);

Think about what you could do with all those extra bytes!

How this works is you convert the number to base 36, so you have the following characters:

0123456789abcdefghijklmnopqrstuvwxyz
^         ^
n        n+10

By offsetting by 10 the characters start at a instead of 0.

Not entirely sure about how fast running the two different examples client-side would compare though.

1
  • 2
    I enjoyed your base 36 creativity
    – Josh
    Nov 16, 2016 at 22:37
9

I don't like all the solutions that use magic numbers like 97 or 36.

const A = 'A'.charCodeAt(0);

let numberToCharacter = number => String.fromCharCode(A + number);

let characterToNumber = character => character.charCodeAt(0) - A;

this assumes uppercase letters and starts 'A' at 0.

6

Javascript's String.fromCharCode(code1, code2, ..., codeN) takes an infinite number of arguments and returns a string of letters whose corresponding ASCII values are code1, code2, ... codeN. Since 97 is 'a' in ASCII, we can adjust for your indexing by adding 97 to your index.

function indexToChar(i) {
  return String.fromCharCode(i+97); //97 in ASCII is 'a', so i=0 returns 'a', 
                                    // i=1 returns 'b', etc
}
1
  • 4
    Well, to be pedantic, it takes a variable number of arguments, not an infinite number.
    – wchargin
    Jan 14, 2015 at 20:05
4

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

var a = String.fromCharCode(97);
4

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).

3
  • That works with ASCII, I need to work with the position of the character in the alphabet. Jun 29, 2010 at 21:54
  • @VIVA - I think you could have worked this out? @Galambalaza - I think you want 65 not 64 Jun 29, 2010 at 21:59
  • i just showed how simple it is. he could've worked this out. but there you go. see the update
    – gblazex
    Jun 29, 2010 at 22:00
1

Try

(n+10).toString(36)

chr = n=>(n+10).toString(36);

for(i=0; i<26; i++) console.log(`${i} => ${ chr(i) }`);

1

The only problem 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

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.

-3

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"

number to letter function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.