Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\mathtt} \newcommand{\hex}{\mathtt} \newcommand{\octal}{\mathtt} \newcommand{\prog}{\mathtt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section4.5Programming Exercise

1

Here is a program that simply reads characters from the keyboard until the user hits the enter (or return) key. Add code to the program so that it allows the user to enter up to a 32-bit integer in hexadecimal and converts the number to int format. Your algorithm should make use of the masking operation described above. The program should display the int in both hexadecimal and decimal format for the user. Assume that the user will not make mistakes.

/* readHex.c
 * Asks user to enter a number in hexadecimal.
 * INCOMPLETE PROGRAM FOR EXERCISE.
 * Bob Plantz - 26 July 2016
 */
#include <stdio.h>
#include <unistd.h>

int main(void)
{
  int x;
   unsigned char aChar;

  printf("Enter an integer in hexadecimal: ");
  fflush(stdout);

  x = 0;                           // initialize result
  read(STDIN_FILENO, &aChar, 1);   // get first character
  while (aChar != '\n')            // look for return key
  {
    read(STDIN_FILENO, &aChar, 1);
  }
   
  printf("You entered %#010x = %d (decimal)\n\n", x, x);

  return 0;
}
Hint Solution