Question: I need to write a C++ program that converts an integer to and from binary, decimal, and hexadecimal. The program must take three command-line arguments:
I need to write a C++ program that converts an integer to and from binary, decimal, and hexadecimal.
The program must 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
Program then prints one of two:
- If the arguments are in the correct format, print the conversion of the number, e.g. 'decimal 16 is binary 10000'
- Otherwise must print a summary of the usage of the program, e.g. Usage: ./hex [ h | d | b ] [ h | d | b]
Example Output Below:

***BONUS POINTS IF: ******
- 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.
***EXTRA BONUS POINTS IF: *****
- No usage of if or switch statements
% ./hex bd 1010 binary 1010 is decimal 10 % ./hex hd 123456 hexadecimal 123456 is decimal 119 3046 % ./hex dh 12345678 decimal 12345678 is hexadecimal bc614e % ./hex a b 123 Usage: ./hex [ h | d | b ] [ h | d | b] % ./hex hd bc614e hexadecimal bc614e is decimal 12345678 % ./hex d h 32 decimal 32 is hexadecimal 20 % ./hex dh 1000000000 decimal 1000000000 is hexadecimal 3b9aca00 % ./hex d b 1000000000 decimal 1000000000 is binary 111011100110101100101000000000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
