I want to convert a number to a char like:
Tried to find anything to help me with but I did not had any lucky. Anybody have a sample for this using JavaScript?
Thank you!
The Letter-to-Number Cipher (or Number-to-Letter Cipher or numbered alphabet) consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .
How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.
The number conversion you've shown doesn't seem consistent. It seems you want to convert a number to the equivalent spreadsheet column letter, similar to this Python question: Convert spreadsheet number to column letter.
Converting that code to javascript (and cleaning up a bit) gives:
function numToSSColumn(num){
var s = '', t;
while (num > 0) {
t = (num - 1) % 26;
s = String.fromCharCode(65 + t) + s;
num = (num - t)/26 | 0;
}
return s || undefined;
}
// A Z AA CZ DA YZ ZZ AAA
[0,1,26,27,104,105,676,702,703,
//AAZ ABA AZZ BAA BAZ BBA YYYZ ZZZZ
728,729,1378,1379,1404,1405,456976,475254].forEach(function(n) {
console.log(n + ' : ' + numToSSColumn(n));
});
The function doesn't check the input and returns undefined if n < 0
.
Your conversions don't seem to work because spreadsheet columns don't start from 0, they start from 1, so A-Z is 1 to 26, AA to AZ is 27 to 52, and so on. 676 is YZ and 456976 is YYYZ. ZZZZ is 475254 (or 11110 base26);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With