CSCE 312, Spring 2022
Computer Organization
Homework 1

Binary, Decimal, and Hexadecimal

Read these instructions carefully. For this assignment, you will write a C++ program that converts integers to and from binary, decimal, and hexadecimal. Write your Your program will take three command-line arguments:

  1. A single character telling what to convert from - h for hexadecimal, d for decimal, or b for binary.
  2. Another character telling what to convert to - again, one of h, d, or b.
  3. A string of digits representing the number to convert from
Your program will print to the standard output one of these: Call your program hex, i.e., the C++ source file would be called hex.cc (or whatever you've been taught is the file extension for C++. Is it C? cxx? cpp? c++?)

Here are some examples of using the program:

% ./hex b d 1010
binary 1010 is decimal 10
% ./hex h d 123456
hexadecimal 123456 is decimal 1193046
% ./hex d h 12345678
decimal 12345678 is hexadecimal bc614e
% ./hex a b 123
Usage: ./hex [ h | d | b ] [ h | d | b]
% ./hex h d bc614e
hexadecimal bc614e is decimal 12345678
% ./hex d h 32
decimal 32 is hexadecimal 20
% ./hex d h 1000000000
decimal 1000000000 is hexadecimal 3b9aca00
% ./hex d b 1000000000
decimal 1000000000 is binary 111011100110101100101000000000

Your program should not print any extraneous information. This is very important; your program should print only what is asked for, nothing more, since we will be grading it using automated scripts maybe. Your program code should fit in a single file and be well-documented so that a reader not familiar with the computer language can still follow the algorithms and intent of your program through your comments. You may assume the input is formatted as non-negative integers in binary, hexadecimal, or decimal.

To turn in your program, upload your one source file to Canvas on or before 11:59pm, September 9. Make sure your program compiles and works on the Linux computers in the CSE department.

Don't collaborate. Don't copy. Read the comments about academic dishonesty in the syllabus.

Hints

Extra Credit

This assignment is a little boring. For you CS geeks, let's make it a little more interesting. You can get 100% extra credit if your program obeys all the original description plus all the following additional rules: Hint: Parameters aren't variables. Recursion can simulate iteration.

More Extra Credit

For 150% extra credit, do all of the above and also do not use any if or switch statements.