views:

101

answers:

5

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 characters position in the alphabet.

Cheers. :)

+1  A: 

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

galambalazs
That works with ASCII, I need to work with the position of the character in the alphabet.
VIVA LA NWO
notice: this will give you the capitals...
Sinan Y.
@VIVA - I think you could have worked this out?@Galambalaza - I think you want 65 not 64
James Westgate
i just showed how simple it is. he could've worked this out. but there you go. see the update
galambalazs
+4  A: 

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);
mbq
Much more elegant than `String.fromCharCode` in my opinion, as as you said, it extends very easily.
musicfreak
And when you have no need of extending, maybe more prone to errors? abcede
Nelson
And it will give `undefined` in case of bad code, which is more sensible that some weird white rectangles.
mbq
@Nelson Thanks, fixed (I hope).
mbq
I think what you really need is write a program that creates this code for you. :P
Nelson
FYI JScript (IE) does not support the index operator `[]` on strings.
Crescent Fresh
@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`](http://bclary.com/2004/11/07/#a-15.5.4.4) is preferred instead of `[]` for browser compatibility. E.g. `'foo'.charAt(0) == 'f'`
CMS
@Crescent, forgot to mention that the `[]` property accessor on strings is standardized on ECMAScript 5 (see [\[\[GetOwnProperty\]\](P)](http://ecma262-5.com/ELS5_Section_15.htm#Section_15.5.5.2)).
CMS
@CMS: thanks for the links. Good to know about IE8.
Crescent Fresh
@Crescent, @CMS, thanks for your comments, I have upgraded the answer to reflect them.
mbq
+4  A: 

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.

Daniel Vandersluis
+1  A: 

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
}
hundredwatt
A: 

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

var a = String.fromCharCode(97);
James Westgate