Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a base 10 number to alphabetic like ordered list in HTML

I want to convert a integer to alphabetic equivalent like ordered list in HTML.

<ol type="a">

I tried to convert a base 10 number to a base 26 with a-z digits.
But that's not what I wanted.

IN     WANT        GET      
-----------------------
1   =>  a       <=  a
2   =>  b       <=  b
3   =>  c       <=  c
4   =>  d       <=  d
5   =>  e       <=  e
6   =>  f       <=  f
7   =>  g       <=  g
8   =>  h       <=  h
9   =>  i       <=  i
10  =>  j       <=  j
11  =>  k       <=  k
12  =>  l       <=  l
13  =>  m       <=  m
14  =>  n       <=  n
15  =>  o       <=  o
16  =>  p       <=  p
17  =>  q       <=  q
18  =>  r       <=  r
19  =>  s       <=  s
20  =>  t       <=  t
21  =>  u       <=  u
22  =>  v       <=  v
23  =>  w       <=  w
24  =>  x       <=  x
25  =>  y       <=  y
26  =>  z       <=  az
27  =>  aa      <=  aa
28  =>  ab      <=  ab
29  =>  ac      <=  ac

private final static char[] digits = {
'0' , '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'
};

private static String numberToAlphaNumeric(long i, int radix) {

    char[] buf = new char[65];
    int charPos = 64;
    boolean negative = (i < 0);
    if (!negative) {
        i = -i;
    }
    while (i <= -radix) {
        buf[charPos--] = digits[(int)(-(i % radix))];
        i = i / radix;
    }
    buf[charPos] = digits[(int)(-i)];
    if (negative) { 
        buf[--charPos] = '-';
    }
    return new String(buf, charPos, (65 - charPos));
}

public static String numberToAlphaNumeric(long number) {
    ArrayList<String> list = new ArrayList<String>();
    for( int j = 0; list.size() != number; j++ ) {
        String alpha = numberToAlphaNumeric( j, digits.length );
        if(!alpha.contains( "0" )) {
            list.add( alpha );
        }
    }
    return list.get( list.size()-1 );
}

My 2nd Idea:

If I extend a new leading symbol to the digits and convert my number to a base 27 number, I have the new Symbol in every carry over which is wrong and I can filter these out.

This is very inefficient and ugly, but I have no more ideas. What is the common way?

like image 863
oliholz Avatar asked Aug 15 '12 13:08

oliholz


2 Answers

This is the basic algorithm. Use a StringBuffer if you need to be more efficient:

  public static String getAlpha(int num) {

    String result = "";
    while (num > 0) {
      num--; // 1 => a, not 0 => a
      int remainder = num % 26;
      char digit = (char) (remainder + 97);
      result = digit + result;
      num = (num - remainder) / 26;
    }

    return result;
  }

Another way to do this would be to convert to base 26, and then add 97 to each character in the string you get.

like image 89
Ben Taitelbaum Avatar answered Sep 26 '22 00:09

Ben Taitelbaum


Store A to Z in array index starting from 1 to 26, say alphArr[]

i = Input

If(i<26){
  Print alphArr[i]
  }else{
  //Consider i=27
  count = i/26  (here, count=1)
  alphabet = i%26  (here alphabet =1)
  print alphArr[count]+””+alphArr[alphabet] // Which will be “AA”
}
like image 25
Bharath Avatar answered Sep 27 '22 00:09

Bharath