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&lt;int&gt;();

var div = myNumber;
var getOne = 0;

while (div &gt; 26)
{
    var value = div % 26;

    value -= getOne;

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

    array.Add(value);

    div = div / 26;
}

div -= getOne;

if (div &gt; 0) array.Add(div);

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

return new string(letters);


Now I wonder if there is a better way write this.
You could slightly simplify the code as follows:


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


The code:

private static string ToBase26(int myNumber)
{
    var array = new LinkedList&lt;int&gt;();

    while (myNumber &gt; 26)
    {
        int value = myNumber % 26;
        if (value == 0)
        {
            myNumber = myNumber / 26 - 1;
            array.AddFirst(26);
        }
        else
        {
            myNumber /= 26;
            array.AddFirst(value);
        }
    }

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