5

Is there a ready made function to be able to do base conversions in c#? I am looking to convert from base 26 and base base 27 numbers to base 10. I can do it on paper but i am not a very experienced programmer and would rather not do it from scratch if possible. Thanks!

miltonjbradley
  • 601
  • 2
  • 10
  • 21
  • 1
    "What have you tried"? (It is desirable to see an attempt, even if not fully working, as then specifics can be discussed and effort has been demonstrated.) –  Mar 04 '12 at 21:50
  • Also, consider looking at *existing* base64 encoders/decoders; it is just slightly different numbers in the math (instead of `/` or `%` by 64, `/` or `%` by 27, etc.), but the idea is the same. –  Mar 04 '12 at 21:52
  • pst: I have not tried anything yet because if something exists I do not want to have to write it. I am not asking for finished code by any means, I am just looking for some direction. Thanks! – miltonjbradley Mar 04 '12 at 21:57
  • Pay heed to the 2nd comment then -- the math is *identical* except for the base used. Also it might take a different lookup to get the numerical value for a specific digit (e.g. if the base-26 number is `A-Z` as base-64 is `0-9A-Z...`). –  Mar 04 '12 at 21:59
  • Somewhat related: http://stackoverflow.com/questions/95105/is-there-any-built-in-way-to-convert-an-integer-to-a-string-of-any-base-in-c –  Mar 04 '12 at 22:01

3 Answers3

5

There is a ready-made function to convert numbers from base 2, 8 or 16 to base 10 ( Convert.ToInt32). If you want to convert numbers from base 26 or base 27 to base 10, you'll have to do it yourself.

Now, I've never heard of base 26 numbers, so I'm just going to assume the 'digits' are A to Z (A having a value of 0, and Z having a decimal value of 25). To convert from base 26 to base 10 you should do the following:

string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int GetDigitValue(char digit)
{
    return charset.IndexOf(digit);
}
int ConvertFromBase26(string number)
{
    int result = 0;
    foreach(char digit in number)
        result = result * charset.Length + GetDigitValue(digit);

    return result;
}

To convert from base 27, just add whatever character represents 26.

Note: There's no error correction (you can convert the string "$#$@#$@" which will get you a nice negative number), and GetDigitValue is rather inefficient and should be replaced with a lookup table if you plan to do these conversions a lot.

EDIT: A LINQ version, just for kicks.

Again, no efficient lookup and no error correction, assuming the string consists only of legal digits.

string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int ConvertFromBase(string charset, string number)
{
    return number.Select(c=>charset.IndexOf(c)).Aggregate(0, (x, y) => x*charset.Length +y);
}

I think the first version is more readable, though.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Technically, `Convert.ToInt32` converts numbers to base 2. It's `Int32.ToString` that converts them to base ten. – dan04 Apr 26 '12 at 22:44
0

Building off of your answer. You don't need a charset lookup list because you can just use the char ASCII Values.

int ConvertFromBase26(string number)
{
     return number.Select(digit => (int)digit - 64).Aggregate(0, (x, y) => x * 26 + y);
}

I used this to convert column string addresses to int in while programming with Excel.

Rafi
  • 2,433
  • 1
  • 25
  • 33
0

Assuming that you use base 26 using alphabet in one based (eg : A = 1, B = 2, ... , Z= 26, AA = 27, ... )

public int ToBase10(this string str)
{
    str = str.ToUpper();
    int number = 0;
    for (int index = 0; index < str.Length; index++)
        number += (str[index] - 'A' + 1) * (int)Math.Pow(26, str.Length - 1 - index);
    return number;
}

To call this function simply call like this

string PlateNumber = "TRX";
int number = PlateNumber.ToBase10();
Fitri Halim
  • 584
  • 4
  • 11