CSCE 312, Fall 2023
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:
- A single character telling what to convert from - h for hexadecimal, d for decimal, or b for binary.
- Another character telling what to convert to - again, one of h, d, or b.
- A string of digits representing the number to convert from
Your program will print to the standard output one of these:
- If the arguments are in the correct format, print the conversion
of the number, e.g. decimal 16 is binary 10000
- Otherwise, print a summary of the usage of the program, e.g.
Usage: ./hex [ h | d | b ] [ h | d | b]
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
- C++ has support for converting between these different formats. Wouldn't it be more fun just to make your own versions of these? You'll learn more.
- There are 3 times 3 = 9 different possible conversions, so you should write 9 different pieces of code, one for each, right? No! Just do three conversions from something to the native int type, then three conversions from the native int type to something.
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:
- It must use only the following library functions: putchar and exit. No STL, no iostream, no cout/cerr, nothing from stdio or stdlib except for putchar and exit, no other pre-defined routines. That means you roll your own code for doing string output, conversion from/to hex, decimal, binary, etc.
- It must use no iteration. That means no for, while, do/while, or goto.
- It must use no variables or assignment statements.
- It must be well documented.
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.