There is a ready-made function for converting 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 will have to do it yourself.
Now I have never heard of 26 base numbers, so I'm just going to assume that the “numbers” are from A to Z (A has a value of 0, and Z has a decimal value of 25). To convert from base 26 to base 10, you must 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, simply add any character representing 26.
Note. There is no error correction (you can convert the string "$ # $@ # $@ ", which will bring you a nice negative number), and GetDigitValue is quite inefficient and should be replaced by a look-up table if you plan to do these conversions a lot.
EDIT: LINQ version, kick only.
Again, no effective search and lack of error correction if the string consists only of legal numbers.
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.
source share