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

2

10

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?

Bremsstrahlung answered 15/8, 2012 at 13:7 Comment(6)
You've shown results that you don't want - only failing on 26, as far as I can tell - but not what the code is...Residuary
Because your 0 should had mapped 'a'Santoyo
I just tested using list-style: lower-alpha, and 26 should be just z, not az.Chui
@Ben Yes I want for 26 just 'z' and for 27 'aa'.Bremsstrahlung
So correct your post. It doesn't say that.Prima
I wanted to map an IntStream to this kind of output. This question helped me to understand that A-Z, AA-ZZ, AAA-ZZZ is not a simply base 26 (since " A" is not the same as "AA"), but also not base 27 (since we don't want " A", "A ", etc).Encumbrance
C
14

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.

Chui answered 15/8, 2012 at 13:36 Comment(0)
S
2

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”
}
Suetonius answered 25/7, 2013 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.