http://www.careercup.com/question?id=6139456847347712
/**
* Here 1..26 is A...Z
* 27...52 is AA...AZ. So we first try to divide num by 26 then find ceiling...so 27...52 returns 2. Then we subtract 1 from the number
* @param num
* @return
*/
public String numberToBase26(int num){
StringBuffer buff = new StringBuffer();
while(num > 0){
char r = convert(num % 26);
buff.append(r);
num = (int)(Math.ceil(num*1.0/26));
num--;
}
return buff.reverse().toString();
}
public int base26ToNumber(String str){
int result = 0;
int pow = 1;
for(int i = str.length() -1; i>=0; i--){
char s = str.charAt(i);
s = (char)(s - 'A'+1);
result += s*pow;
pow *= 26;
}
return result;
}
private char convert(int r){
if(r == 0){
return 'Z';
}else{
return (char)(r + 'A'-1);
}
}
Microsoft Excel numbers cells as 1...26 and after that AA, AB.... AAA, AAB...ZZZ and so on.
Given a number, convert it to that format and vice versa.
https://github.com/mission-peace/interview/blob/master/src/com/interview/misc/ConvertNumberIntoBase26.javaGiven a number, convert it to that format and vice versa.
/**
* Here 1..26 is A...Z
* 27...52 is AA...AZ. So we first try to divide num by 26 then find ceiling...so 27...52 returns 2. Then we subtract 1 from the number
* @param num
* @return
*/
public String numberToBase26(int num){
StringBuffer buff = new StringBuffer();
while(num > 0){
char r = convert(num % 26);
buff.append(r);
num = (int)(Math.ceil(num*1.0/26));
num--;
}
return buff.reverse().toString();
}
public int base26ToNumber(String str){
int result = 0;
int pow = 1;
for(int i = str.length() -1; i>=0; i--){
char s = str.charAt(i);
s = (char)(s - 'A'+1);
result += s*pow;
pow *= 26;
}
return result;
}
private char convert(int r){
if(r == 0){
return 'Z';
}else{
return (char)(r + 'A'-1);
}
}