Programming Assignment 1

Due Thursday, September 20
15 points


For this assignment, you should write a program that converts unsigned (non-negative) numbers between hexadecimal and decimal. Although programming languages may provide easier ways to do this, in order to get credit you must use the repeated multipication and division algorithms described in Unsigned Integer Coding.

Program Input and Output

The program should be designed to read lines from input, producing a single line of output for each line. The first character of an input line should be a character indicating the direction of conversion, 'd' for converting hexadecimal to decimal, 'h' for converting from decimal to hexadecimal. That is, the first character indicates the form of the result of the conversion.

The first character may be followed by 0 or more "white" characters. The remaining characters should be hexadecimal characters if the first character is 'd', or decimal characters if the first character is 'h'.

If an input line is incorrect the next output line from your program should just be an error message indicating incorrect input format. Otherwise, the next output line should be the appropriately converted number.

Program Structure

Input to and output from a program are in character form so your program will need two functions hex2dec() and dec2hex() that have character string parameters and return character strings. The first converts a string containing hexadecimal digits ('0' to '9', 'a' to 'f', and 'A' to 'F') to a string containing decimal digits ('0' to '9'). The second converts a string of decimal digits to a string of hexadecimal digits.

To avoid the necessity of dynamic memory allocation, your program can declare a single character string variable that is used as the returned value from each of these functions. Don't forget to put a null character at the end of your output string to terminate the string.

For either decimal to hexadecimal or hexadecimal to decimal conversion, you will need to convert the input string to a C int, then convert back to a string of digits. You may find it useful to define functions for the following operations.

Hexadecimal to decimal conversion uses the first two and decimal to hexadecimal conversion uses the last two.

Digit Values for Single Characters

In the repeated multipication and division algorithms, you need to use integer values for digits, but the input or output needs to be a character. In C, characters use ASCII coding, which assigns an integer code to each character. C allows you to do arithmetic directly with these integer codes. The following tables describe how to convert a character to its numerical value and vice-versa.

For an input character ch, its digit value is given by the expression in the following table.

Range of ch Digit Value
'0' to '9' ch - '0'
'a' to 'f' ch - 'a' + 10
'A' to 'F' ch - 'A' + 10

For digit value d, its output character value is given by the expression in the following table.

Range of d Digit Value
0 to 9 d + '0'
10 to 15 d + 'a'

There is enough complexity in these character conversions that you will find it worthwhile to encapsulate them as functions. The conversion described by the first table can be used for processing input characters for either kind of conversion. However, for decimal to hexadecimal conversion, you will have to check the digit value to make sure it is between 0 and 9. The conversion described by the second table can also be used in either kind of conversion. If the repeated multipication or division algorithm is correct you will not need to check the result.

What to Turn In

You should turn in a listing of your program along with a recording of its execution with sample input that shows multiple digit conversions in both directions, along with input that demonstrates error handling. Your program listing should contain your name and the course number. Your code should be commented with at least a description of what it does and it should have comments for each of the functions.