
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Convert number to characters in JavaScript
Suppose we have a login system where users are required to provide a unique userID to uniquely identify them. And for obvious security reasons, we want to obfuscate the userID so that it is fairly hard to guess the number.
Basically, we are required to write two functions.
encode(),
decode()
The first function should convert our number input into a base 26 alphabet mapping. And the second function should convert the alphabet mapping back into the original number.
Although this isn't very practical as most of the Hashing algorithms do not provide to and fro conversions, but for the purpose of this question, we are not considering that.
For example − If the number is 31, then,
encode(31) = 'ea' and, decode('ea') = 31
Example
The code for this will be −
const num = 31; const encode = num => { let res = ''; const legend = 'zabcdefghijklmnopqrstuvwxy'; while(num){ const rem = num % (legend.length); res += legend[rem]; num = (Math.floor(num / legend.length)); }; return res; }; const decode = str => { let num = 0; const legend = 'zabcdefghijklmnopqrstuvwxy'; for(let i = 0; i < str.length; i++){ const ind = legend.indexOf(str[i]); num += (ind * Math.pow(legend.length, i)); }; return num; } console.log(encode(num)); console.log(decode('ea'));
Output
And the output in the console will be −
ea 31
- Related Articles
- How to convert Unicode values to characters in JavaScript?
- How to convert special characters to HTML in Javascript?
- How to convert Boolean to Number in JavaScript?
- How to convert Number to Boolean in JavaScript?
- How to convert Number to String in JavaScript?
- How to convert String to Number in JavaScript?
- Convert number to alphabet letter JavaScript
- How to convert a value to a number in JavaScript?
- Convert list of strings and characters to list of characters in Python
- Number of non-unique characters in a string in JavaScript
- Convert List of Characters to String in Java
- Convert number to reversed array of digits JavaScript
- Convert number to a reversed array of digits in JavaScript
- How to convert a number into an array in JavaScript?
- How to convert a string with zeros to number in JavaScript?

Advertisements