Convert numbers to letters javascript decode

JavaScript

In this article, we are going to learn how to convert numbers to letters javascript decode with example. We are going to use fromCharCode() to Convert numbers to Letters and charCodeAt() method and also learn how to convert number to letter alphabet.

What is JavaScript fromCharCode()


The fromcharcode() is a static method of string object that returns string after converting Unicode values to characters.

Syntax

string.fromCharCode(num)
string.fromCharCode(num....numn)

Parameter

Num…numN: The sequence of numbers that are UTF-16 unit code. It has range 0 and 65535(0xFFFF) number greater than this range is truncated.

1.Convert number to Letters uppercase


The upper case letter ASCII value range starts from 65 to 90. We are using for loop that starts from 65 value of letter A and so on and ends at 90 value of letter Z.

  • The javascript fromCharCode() method changing num value to charcater and appending to Str.After loop end we are printing the result using console.log(str);
  • We have also passed multiple sequence values to fromCharCode( ) to print a word.

JavaScript Example to numbers to Letters

var str = "";
for (let num = 65; num <=90 ; num++)
{
 
 let char = String.fromCharCode(num);
 str += char + ", ";
}

console.log(str);



let char = String.fromCharCode(68, 69, 86, 69, 78, 86, 77);
 
 
console.log(char)


//sequence of mutiple number value
let letter = String.fromCharCode(68, 69, 86, 69, 78, 85, 77);
 

 
console.log(letter )


Output

A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,

DEVENUM

2. Convert Number to letter lower case


The lower case letter ASCII value range starts from 97 to 122. We are using for loop that starts from 97 value of letter a and so on and ends at 122 value of letter z.

The javascript fromCharCode() method changes num value to the character and appending to Str.After loop ends we are printing the result using console.log(str);

JavaScript Example to numbers to Letters

var str = "";
for (let num = 97; num<=122 ; num++)
{
 
 let char = String.fromCharCode(num);
 str += char + ", ";
}

console.log(str);

Output

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 

3. Convert specific number to letter upper or lower case


To print a specific number ASCII value to upper-case or lower case letter. We simply add the number value to 64 for the upper case or add the number value to 96 for the lower case as we have done in the below example.

JavaScript Program Example

function numto_Uper_lower_char(mynum)
{

let upper_case = String.fromCharCode(64 + mynum)
let lower_case = String.fromCharCode(96 + mynum)
console.log('upper case :',upper_case)
console.log('lower case',lower_case)
}

numto_Uper_lower_char(13)

Output

upper case : M
lower case m

4. Convert Number to letter of alphabet


Sometimes we need to convert the number to alphabet 1 to 26. We have touppercase() to convert the number to letter of alphabet uppercase and toLowerCase() number to convert letter to lower case.

JavaScript Program Example

var num = 26;
console.log((num + 9).toString(36).toUpperCase());

//output
Z
var num = 1;
console.log((num + 9).toString(36).toLowerCase());
//Output
a

Another way to Convert Numbers to the letter of the alphabet in upper case and lower case.

JavaScript Program Example

//number to upper case alphabet
const num_alpha = (num) => {
   if(num < 1 || num > 26 || typeof num !== 'number'){
      return -1;
   }
   const Startval = 64;
  
   return String.fromCharCode(num + Startval);
};

console.log(num_alpha(12));

//output
L


//number to lower case alphabet

const num_alpha_lower = (num) => {
   if(num < 1 || num > 26 || typeof num !== 'number'){
      return -1;
   }
   const Startval = 96;
  
   return String.fromCharCode(num + Startval);
};

console.log(num_alpha_lower (12));


//output
l


5. Convert Letter to Number Using charCodeAt() method


To convert letter to its ASCII value JavaScript charCodeAt() method is used.The charCodeAt() get unicode value of character at given index in a string.It is static method of string object it always call with string object. string.charCodeAt(0)

Syntax

string.charCodeAt(index)

Parameter

  • index : The index will between 0 and 1 less than length of string.

JavaScript Program Example

function numto_char(character)
{
 
let char_upper = character.toUpperCase().charCodeAt(0)
let char_lower = character.toLowerCase().charCodeAt(0)


console.log('lower-case char ASCII value is :',character.toLowerCase(),char_lower)
console.log('Upper-case char ASCII value is :',character.toUpperCase(),char_upper)

}

numto_char('g')

Output

lower-case char ASCII value is : g 103
Upper-case char ASCII value is : G 71

Summary

In this post, we have learned to Convert numbers to letters javascript decode and vice versa using fromCharCode() and charCodeAt() methods.