Converting Numbers Between Bases in .NET
I was goofing off last night when I was waiting for my wife to get out of class and came up with BaseMaster. BaseMaster is a simple online base conversion utility that takes a (valid) number from one base and converts it to another.
It turns out that converting numbers between bases is extremely easy in .NET. Microsoft was nice enough to overload the
Convert.ToInt64(num, base) method to allow conversions to base 10. Conversely, the
Convert.ToString(num, base) medhod will convert a base 10 number to a specified base. The only catch is that the bases have to be
2, 8, 10, or 16
These two methods allow a pretty easy conversion method to be written that will handle all of the conversions I'm interested in.
The key here is to convert the user-input number to base 10 and then convert the base 10 number to the desired base. This way you don't have to worry about catching every special case where a user would want to convert from one base to another.
System.FormatException will get thrown if the input number has invalid characters for it, so you should keep that in mind if you ever need to do something similar.
BaseMaster is free to use and enjoy.