-4

I'm looking for a routine in C# that gives me the following output when putting in numbers: 0 - A 1 - B 2 - C 3 - AA 4 - AB 5 - AC 6 - BA 7 - BB 8 - BC 9 - CA 10 - CB 11 - CC 12 - AAA 13 - etc

I'm using letters, so that it's not so confusing with zero's. I've seen other routines, but they will give me BA for the value of 3 and not AA.

Note: The other routines I found was Quickest way to convert a base 10 number to any base in .NET? and http://www.drdobbs.com/architecture-and-design/convert-from-long-to-any-number-base-in/228701167, but as I said, they would give me not exactly what I was looking for.

  • 1
    doesn't AA equals to 0 in your logic? – Abdul Hameed Mar 27 '18 at 13:12
  • 1
    if A is zero, then AA will also be zero in all bases right..? – Abdul Hameed Mar 27 '18 at 13:12
  • 1
    That's not how traditional number formats work. Otherwise we'd have 00 instead of 10. But nothings stopping you from attempting to write code that will do that and then asking for help when you run into problems. – juharr Mar 27 '18 at 13:14
  • 1
    This is basically a variantion on the way Excel generates column names and thus https://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa may be a good reference for you. Just convert that to use three letters instead of 26 and you should be good. – Chris Mar 27 '18 at 13:16

2 Answers2

1

Converting between systems is basic programming task and logic doesn't differ from other systems (such as hexadecimal or binary). Please, find below code:

//here you choose what number should be used to convert, you wanted 3, so I assigned this value here
int systemNumber = 3;
//pick number to convert (you can feed text box value here)
int numberToParse = 5;
// Note below
numberToParse++;
string convertedNumber = "";
List<char> letters = new List<char>{ 'A', 'B', 'C' };
//basic algorithm for converting numbers between systems
while(numberToParse > 0)
{
    // Note below
    numberToParse--;
    convertedNumber = letters[numberToParse % systemNumber] + convertedNumber;
    //append corresponding letter to our "number"
    numberToParse = (int)Math.Floor((decimal)numberToParse / systemNumber);
}
//show converted number
MessageBox.Show(convertedNumber);

NOTE: I didn't read carefully at first and got it wrong. I added to previous solution two lines marked with "Note below": incrementation and decrementation of parsed number. Decrementation enables A (which is zero, thus omitted at the beginning of numbers) to be treated as relevent leading digit. But this way, numbers that can be converted are shifted and begin with 1. To compensate that, we need to increment our number at the beginning.

Additionaly, if you want to use other systems like that, you have to expand list with letter. Now we have A, B and C, because you wanted system based on 3. In fact, you can always use full alphabet:

List<char> letters = new List<char> {'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'};

and only change systemNumber.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • The output this produces doesn't match what the OP is after. Do read his specification of his output and not just the "base 3" from the title. In particular yours generates for 1 up: B, C, BA, BB, BC, CA, CB, CC, BAA... The OP wants A, B, C, AA, AB, AC, BA, BB, BC... – Chris Mar 27 '18 at 13:41
  • Thanks. This gave me the best answer, since it also was easy to change the letters. Though the first solution should also be easy to alter to work the same way. – Hans Egil Vaaga Mar 27 '18 at 19:36
1

Based on code from https://stackoverflow.com/a/182924 the following should work:

private string GetWeirdBase3Value(int input)
{
    int dividend = input+1;
    string output = String.Empty;
    int modulo;

    while (dividend > 0)
    {
        modulo = (dividend - 1) % 3;
        output = Convert.ToChar('A' + modulo).ToString() + output;
        dividend = (int)((dividend - modulo) / 3);
    } 

    return output;
}

The code should hopefully be pretty easy to read. It essentially iteratively calculates character by character until the dividend is reduced to 0.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • 1
    Well, I have to strike back :) Your solution doeasn't work for `dividend = 0`, which should return `A`. Your solution would return empty string, thus your code isn't complete :) Have a nice day – Michał Turczyn Mar 27 '18 at 14:43