3

I am trying to develope a routine in C# that will take a given input integer and return a 6 character alpha numeric string based on a predefined possible set of characters.

The possible characters to use are:

"0123456789ABCDEFGHJKLMNPQRSTUVWXYZ" (note that the letter "I" and "O" are not in the set.)

Therefore given the input of 1, the output should be "000001", input of 9 would output "000009", input of 10 would output "00000A", input of 12345 would output "000AP3", and so on.

I am having a hard time coming up with an elegant solution to this problem. I know I must be approaching this the hard way so I'm looking for some help.

Thanks!

Richard West
  • 2,166
  • 4
  • 26
  • 40
  • 2
    I can see you want leading 0's, but how did you arrive at that AP3 from 12345? That is what is needed to come up with some solution. – Hans Kesting Sep 23 '10 at 15:41
  • 1
    Have a look at [zBase32](http://mytenpennies.wikidot.com/blog:base-32-encoder) [("human-oriented base-32 encoding")](http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt). – dtb Sep 23 '10 at 15:43

4 Answers4

7
int value = 12345;

string alphabet = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";

var stack = new Stack<char>();
while (value > 0)
{
     stack.Push(alphabet[value % alphabet.Length]);
     value /= alphabet.Length;
}
string output = new string(stack.ToArray()).PadLeft(6, '0');
LukeH
  • 263,068
  • 57
  • 365
  • 409
1

The direct solution would simply be to iteratively divide your input value by N (the size of the character set), and take the remainder each time to index into the character set, and build up the output string character-by-character.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0
internal class Program {
    private static void Main(string[] args) {
        int value = 38;

        const string alphabet = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
        string result = ToBase(value, alphabet);
        Console.WriteLine(result);
    }

    private static string ToBase(int value, string alphabet) {
        if (value == 0) return alphabet[0].ToString();
        var result = new StringBuilder();
        while (value > 0) {
            int digit = value % alphabet.Length;
            value = (value - digit) / alphabet.Length;
            result.Insert(0, alphabet[digit]);
        }
        return result.ToString();
    }
}

you do zero-padding

THX-1138
  • 21,316
  • 26
  • 96
  • 160
0

LukeH answer modified for Generating Alphanemuric to numeric and vice versa

    int value = 12345;

    string alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var stack = new Stack<char>();
    while (value > 0)
    {
        int index = value % alphabet.Length;
        stack.Push(alphabet[index]);
        value /= alphabet.Length;
    }

    string output = new string(stack.ToArray()).PadLeft(6, '0');
    double intNumber = 0;
    int charPos = 0;
    for (var i = output.Length-1; i >=0;i--)
    {
        int val = output[i];
        if (val >= 48 && val <= 57)
            intNumber += (val - 48) * (Math.Pow(36, charPos++));
        else if (val >= 65 && val <= 90)
            intNumber += (val - 55) * (Math.Pow(36, charPos++));
    }
X-Coder
  • 2,632
  • 2
  • 19
  • 17