In this exercise we will write a program that can convert between decimal and hexadecimal notation of numbers. Please consult the focus boxes about hexadecimal numbers in the text book version if you need to. You might expect that this functionality is already present in the C# libraries. And to some degree, it is. The static method ToInt32(string, Int32) in class Convert converts the string representation of a number (the first parameter) to an arbitrary number system (the second parameter). Similar methods exist for other integer types. The method ToString(string) in the struct Int32, can be used for conversion from an integer to a hexadecimal number, represented as a string. The parameter of ToString is a format string. If you pass the string "X" you get a hexadecimal number. The program below shows examples:
Now, write a method which converts a list (or array) of digits in base 16 (or more generally, base b, b >= 2) to a decimal number. The other way around, write a method which converts a positive decimal integer to a list (or array) of digits in base 16 (or more generally, base b). Here is an example where the requested methods are used:
|