Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numbers to letters beyond the 26 character alphabet

Tags:

I'm creating some client side functions for a mappable spreadsheet export feature.

I'm using jQuery to manage the sort order of the columns, but each column is ordered like an Excel spreadsheet i.e. a b c d e......x y z aa ab ac ad etc etc

How can I generate a number as a letter? Should I define a fixed array of values? Or is there a dynamic way to generate this?

like image 284
Chris Spittles Avatar asked Nov 23 '11 10:11

Chris Spittles


People also ask

How do I get the alphabet value in Excel?

The cell you've selected will contain #N/A because you haven't told Excel which letter to search for. Go to the formula in the bar, click between the quotation marks and type the letter C. Your formula should now read: =LOOKUP("C",A2:B6). Press Enter and the value of the letter.

How to convert a number to a letter of the alphabet?

If want to convert a number to its corresponding alphabet letter like this:- 1 = A 2 = B 3 = C You will need to write a loop, and mod (%) the number by 26 each time through and use the following: String.fromCharCode(num + 64); Convert number to a letter of the alphabet JavaScript example HTML example code:

How many letters are in the alphabet 25 or 26?

I could be wrong but I've checked the other functions in this answer and they seem to fail at 26 which should be Z. Remember there are 26 letters in the alphabet not 25. Show activity on this post.

How do you convert letters to numbers in cryptography?

Numbering the letters so A=1, B=2, etc is one of the simplest ways of converting them to numbers. This is called the A1Z26 cipher. However, there are more options such as ASCII codes, tap codes or even the periodic table of elements to decode numbers.

What is the number a1z26?

Letter Number (A1Z26) A=1, B=2, C=3. Tool to convert letters to numbers and vice versa. The Letter-to-Number Cipher (or Number-to-Letter Cipher) consists in replacing each letter by its position in the alphabet, for example A=1, B=2, Z=26, hense its over name A1Z26.


2 Answers

I think you're looking for something like this

    function colName(n) {          var ordA = 'a'.charCodeAt(0);          var ordZ = 'z'.charCodeAt(0);          var len = ordZ - ordA + 1;                  var s = "";          while(n >= 0) {              s = String.fromCharCode(n % len + ordA) + s;              n = Math.floor(n / len) - 1;          }          return s;      }    // Example:        for(n = 0; n < 125; n++)              document.write(n + ":" + colName(n) + "<br>");
like image 109
georg Avatar answered Sep 19 '22 15:09

georg


This is a very easy way:

function numberToLetters(num) {     let letters = ''     while (num >= 0) {         letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[num % 26] + letters         num = Math.floor(num / 26) - 1     }     return letters } 
like image 37
gooostaw Avatar answered Sep 19 '22 15:09

gooostaw