Computer Science 111a

Lab #9

November 19, 2003

The purpose of today's lab is to work with conversions between character strings and integers.

Get a copy of labs/lab9.  Your task today is to write this program.

Your program should:

·        Print an introduction.

·        Ask the user to enter a hexadecimal integer, and scan it in as a string.

·        Call a function that will check that the string represents a legal hexadecimal integer.  The function should check for an initial + or -, and skip past it if necessary.  Then it should check for legal hexadecimal digits (the letters can be upper or lower case) followed by the null character.  The function should return 1 (true) if the string does represent a legal hexadecimal integer, and 0 (false) if it does not.

·        Call a function that will convert the string to an int value.   The function can do this conversion as I showed you in class, except that you also need to account for a possible '-'sign at the beginning of the string.  So: start at the beginning of the string or at the next byte if there is a positive or negative sign, and get the int value as a positive number.  Then check whether there is a '-' sign at the beginning of the string, and if there is, convert the int value to a negative value.  Finally, the function should return the int value.

·        Call a function that will convert the int value to a base 10 string.  The function can do this conversion as I showed you in class, except that you will need to check for a negative int value.  If it is negative, set a variable to record this fact, then convert the int value to a positive value and produce the string of digits representing that positive value, and put a '-' sign at the beginning of the string.

·        Print the base 10 string.

·        Call the same function to convert the int value to a hexadecimal string.

·        Print the hexadecimal string.

You are now ready to do homework #9.