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!
Share
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:
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.
I think the first version is more readable, though.