JavaScript

Convert Integer to Its Character Equivalent in JavaScript

The process of converting an integer to its equivalent character or the other way around assists in accessing the alphabetic characters and the numbers instantly. For instance, this technique can be of great aid in designing a confidential combination or encoding the data. Also, it is of great aid in reducing the overall code complexity as well.

This tutorial will discuss the approaches to converting an integer to its character equivalent using JavaScript.

How to Convert/Transform Integer to Its Character Equivalent Using JavaScript?

To convert an integer to its equivalent character in JavaScript, apply the combined “charCodeAt()” and “String.fromCharCode()” methods. The charCodeAt() method gives the character’s Unicode at a specific index in a string, whereas the String.fromCharCode() method transforms the Unicode values into characters. These methods can be applied in combination to return the corresponding character against the passed integer with respect to the specified character via a user-defined function.

Syntax

string.charCodeAt(index)

In the above syntax:

index” refers to the character’s index.

String.fromCharCode(num1, num2)

In the given syntax:

num1”, “num2” correspond to one or more Unicode values to be converted.

Example 1: Convert Integer to Its Character Equivalent (Lowercase) Using JavaScript
In this example, the passed integer will be converted into the equivalent character in lowercase:

<script type="text/javascript">
function convertintChar(integer) {
 let character = 'a'.charCodeAt(0);
 console.log("The character code is:", character);
 return String.fromCharCode(character + integer);
}
console.log("The character equivalent of the integer is:", convertintChar(2));
</script>

In the above lines of code:

  • Define a function named “convertintChar()” having the stated parameter.
  • The function parameter points to the integer, which needs to be converted into its equivalent character.
  • In the function definition, specify the stated character and apply the “charCodeAt()” method having “0” as its parameter, which points to the character’s index.
  • This method will return the Unicode of the associated character and display it.
  • After that, apply the “String.fromCharCode()” method to convert the computed Unicode value, in the previous step, into a character.
  • The “+” sign in the method’s parameter indicates that the passed integer will be added to the specified character discussed before and return the corresponding character with respect to it.
  • Lastly, access the defined function by passing the stated number to perform the desired requirement.

Output

In the above output, the integer “2” is converted into its equivalent character “c”. Note that 0,1,2 correspond to the characters “a”, “b”, “c”, and so on.

Example 2: Convert Integer to Its Character Equivalent(Uppercase) Using JavaScript
In this particular example, likewise, the passed integer will be converted into its equivalent character but in the upper case:

<script type="text/javascript">
function convertintChar(integer) {
 let character = 'A'.charCodeAt(0);
 console.log("The character code is:", character);
 return String.fromCharCode(character + integer);
}
console.log("The character equivalent of the integer is:", convertintChar(0));
</script>

Perform the following steps, as given in the above code:

  • Define a function having the stated parameter, as we did in the previous example.
  • In its definition, specify the character in the upper case and associate it with the “charCodeAt()” method, as discussed before.
  • Then, repeat the discussed approaches as stated previously for converting the passed integer “0” into its character equivalent.

Output

In the above output, the character code of “A” is 65, and the equivalent character of the passed integer “0” is “A”.

Example 3: Convert Character Back to Its Integer Equivalent Using JavaScript
If there is a requirement to convert the character back to its equivalent integer, follow the below-stated steps:

<script type="text/javascript">
function convertcharInt(ch) {
 let character = 'a'.charCodeAt(0);
 console.log("The character code is:", character);
 return ch.charCodeAt(0) - character;
}
console.log("The integer equivalent of the character is:", convertcharInt('a'));
</script>

Implement the following steps, as given in the above code:

  • Define a function named “convertcharInt()” having the given parameter, which corresponds to the passed character that needs to be converted into the equivalent integer.
  • In the function definition, similarly, return the Unicode of the associated character and display it.
  • Also, subtract the character code of the character “a” from the character code of the passed character to fetch the character’s equivalent integer.
  • Lastly, access the defined function by passing the character “a” to get its equivalent integer.

Output

The above output signifies that the desired functionality is achieved.

Conclusion

The “charCodeAt()” and the “String.fromCharCode()” methods can be implemented in combination to convert the integer to its character equivalent in JavaScript. These methods are utilized to return both the lowercase and uppercase characters corresponding to the passed integers. This blog is guided to convert/transform an integer to its equivalent character in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.