# Converting to Base 26 in One Based mode

I needed to create a simple method that converts numbers in Base 26 (string) using, as expected, letters from a to z.

But that's not all, this Base 26 needs to be One Based. Which means that there is no representation for zero absolute value and there is a representation for 26 absolute value. Let me show a example:

In Base 26 Zero Based the number 18252 will be represented like:

$$(18252)_{10} = (BBAA)_{26}$$

where B == 1 and A == 0. But as an One Based I need to get:

$$(18252)_{10} = (ZYZ)_{26}$$

where Y == 25 and Z == 26.

So I came up with this snippet:

var array = new List<int>();

var div = myNumber;
var getOne = 0;

while (div > 26)
{
var value = div % 26;

value -= getOne;

if (value <= 0)
{
value += 26;
getOne = 1;
}
else
{
getOne = 0;
}

div = div / 26;
}

div -= getOne;

var letters = array.Select(s => (char)('A' + s - 1)).Reverse().ToArray();

return new string(letters);


Now I wonder if there is a better way write this.

• It looks like you are trying to get an excel column name from a number e.g. 1 => A, 2 => B, ... 26 => Z, 27 => AA, 28 => AB. Is this correct? If so, are you happy with a solution that works as far as 16384 (the max number of columns in an Excel spreadsheet)? May 12 '15 at 15:59
• Yes, can be like Excel May 12 '15 at 16:13

You could slightly simplify the code as follows:

• Reduce the number of variables.
• Use the LinkedList<T> (and the AddFirst method) instead of the List<T> to eliminate reversion of array.
Alternatively you can use the List<T>.Insert method to achieve the same result.

The code:

private static string ToBase26(int myNumber)
{

while (myNumber > 26)
{
int value = myNumber % 26;
if (value == 0)
{
myNumber = myNumber / 26 - 1;
}
else
{
myNumber /= 26;
}
}

if (myNumber > 0)
{
}
return new string(array.Select(s => (char)('A' + s - 1)).ToArray());
}


You could reduce the number of variables, and use LinkedList<T> method to eliminate reversion, like this:

private static string ToBase26(int number)
{

• And please perform some tests before posting. For instance, make sure that this code returns ZYZ for 18252. May 13 '15 at 12:47