Question

C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks in advance, any help would be greatlyappreciated, especially how I would go about forming the loop needed to read in each value and then print the converted value in the next column.

Code Requirements and Specifications

Do not use any "using" directives in your code.

The following constants should be declared and used in bases.cpp:

static const unsigned short
cwh = 16, // max digits of unsigned long hexadecimal
cwd = 20, // max digits of unsigned long decimal
cwo = 26, // max digits of unsigned long octal
cs1 = 4, cs2 = 2// col spacers

Do not hard code literal values for column widths and column spacers. These constants are set up to produce tables when the integers are 64-bit (eight byte) words(the size of type unsigned long). The various cw values are the max size of a 64-bit number in the various notations. The cs values are the spacing preceeding thecolumn.

The application bases should take two file names at the command line. The first is an input file, which contains documentation, instructions, and data. The secondis an output file in which a table should be created according to the input file instructions and data.

Note that there are options specifying the numerical notation for input and for the notation in two columns of output. The output file should be a table withheader (giving the names of the output file and the input file) and column headers indicating the notation used in that column. The data in the table consists ofone line for each number in the input file, in the notation specified by the formatting characters. See bases.x and the example input data files for clarificationof this behavior.

The input file is formatted as follows:

input file format:
=================

1: file documentation lines at top of file begin with '#'

2: next string of data: (five format control characters)

format for input file
first character - how to interpret input numbers:
'o' or 'O': read in octal notation
'd' or 'D': read in decimal notation
'h' or 'H': read in hexadecimal notation

next characters format output, read in pairs
(two for col 1, two for col 2)

first character of pair: numerical notation
'd' = decimal
'D' = decimal
'h' = hex/lower case
'H' = hex/upper case
'o' = octal
'O' = octal
second character of pair: fill (affects hex and oct only)
'f' = fill hex and octal with leading zero, 64-bit
'n' = no fill for hex and octal

3: remainder of input file: unsigned long integers in notation designated by first format control character

The program should skip the documentation, then read the five formatting characters, and then proceed to read the file data, constructing the table in the outputfile as it goes. (There is no need to store more than one integer at a time, just read an integer and then write the line of the output file table corresponding tothat input.)

The output file should begin with two lines of information giving the names of the file and the input data file. The third line should be blank, and the fourthline should have the headers for the table columns. The actual table entries should commence below, and allign correctly with, the column headers.

Be sure that hex and octal numbers are output with the correct number of leading zeros in case 'f'. The defined constants cwx above give the correct size neededfor 64-bit numbers.

When in doubt, use the distributed executables for guidance in constructing the output file.


Example of input file and output:

Here is the input file data1.in:

# data1.in
# read data as decimal numbers
# convert these numbers to a table of dec, hex values
#

ddnhf
0
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
4294967295
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000
18446744073709551615

Here is the outputfile data1.out:

Name of this file: data1.out
Name of input data file: data1.in

dechex
00000000000000000
10000000000000001
10000000000000000a
1000000000000000064
100000000000000003e8
100000000000000002710
10000000000000000186a0
100000000000000000f4240
100000000000000000989680
1000000000000000005f5e100
1000000000000000003b9aca00
429496729500000000ffffffff
1000000000000000002540be400
100000000000000000174876e800
1000000000000000000e8d4a51000
10000000000000000009184e72a000
10000000000000000005af3107a4000
100000000000000000038d7ea4c68000
10000000000000000002386f26fc10000
100000000000000000016345785d8a0000
10000000000000000000de0b6b3a7640000
100000000000000000008ac7230489e80000
18446744073709551615ffffffffffffffff


0 0
Add a comment Improve this question Transcribed image text
Answer #1
//****************************************************************************//
// //
// Program Name : ConvDec.cpp //
// //
// Programmer : K.Angel //
// : Novice C++ //
// //
// Description : The user is asked to input a number in either Hex, Octal or //
// Decimal, which is then converted to Hex, Octal &Decimal. //
// The user can exit the program from the MAIN MENU or by //
// the Escape key after each convertion. //
// //
// Compiler : Borland Turbo C++ v4.5. //
// Date : 16/03/2006 //
// //
//****************************************************************************//

#include <iostream.h>
#include <conio.h> // for clrscr() &getche() functions...
#include <stdlib.h> // for exit() function...

const int ESC = 27; // Used for escape sequince

int main()
{
int key = 0;
int choise = 0;
long number;

while(key != ESC)
{
cout << "Choose Your Number Basen";
cout << "nt1. Hexadecimal.";
cout << "nt2. Octal.";
cout << "nt3. Decimal.";
cout << "nt4. Exit.nt ";
cin >> choise;

switch(choise)
{
case 1: clrscr();
cout << "Enter a Hex number: ";
cin >> hex >> number;
cout << "n value in octal = "
<< oct << number << endl;
cout << " value in decimal = "
<< dec << number << endl;
break;
case 2: clrscr();
cout << "Enter a Octal number: ";
cin >> oct >> number;
cout << "n value in hex = "
<< hex << number << endl;
cout << " value in decimal = "
<< dec << number << endl;
break;
case 3: clrscr();
cout << "Enter a dec number: ";
cin >> dec >> number;
cout << "n value in octal = "
<< oct << number << endl;
cout << " value in hex = "
<< hex << number << endl;
break;
case 4: clrscr();
cout << "Program terminated by user...";
exit(0);
break; //Unreachable code....
default : clrscr();
cout << "ERROR ~ Invalid selectionnn";
break;
}
cout << "Press any key to continue or 'Esc' to exit";
key = getche();
clrscr();
}
cout << "Program terminated by user...";
return 0;
}
answered by: blvcf
Add a comment
Know the answer?
Add Answer to:
C++ program to convert between decimal, hexadecimal, and octal. Please Help!!
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Description For this program, you are going to convert decimal (integer) numbers into their octal number...

    Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • C++ please! In this program you will be outputting the characters that map to the ASCIl...

    C++ please! In this program you will be outputting the characters that map to the ASCIl codes 32 through 126. You will need a loop to iterate through the input values and output the corresponding character. This mapping is shown in appendix A in your Gaddis text book. Your program will be reading in two unsigned integer values. The prompt for read will be Enter lower and upper values You need to check that both values are in the range...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • Please complete the following problem in C. No source code is provided. The left-shift operator can...

    Please complete the following problem in C. No source code is provided. The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned int variable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • **URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input...

    **URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input file and solves them; needs order of operations) Write a MIPS Assembly Language program to read hexadecimal values and operations (one per line) from a file. The file name should be requested and read in from the console. Once the ascii number has been read in, convert it to internal, binary, storage (decimal). The input format may be either fixed-format, 32-bit, twos compliment values,...

  • C PROGRAM When you print out the old and new bitsets, which are of type unsigned...

    C PROGRAM When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x. unsigned char bitset = 0x14 ; printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what...

  • Please help me with the following C Programming project. I am providing the code I used...

    Please help me with the following C Programming project. I am providing the code I used which needs to be reworked to add the following requirements. Here is my code #include<stdio.h> char input; int main() { int i = 0; while (true){ scanf_s("%c", &input); if (input == 'X') break; printf("%c", input); } return 0; } Here are the requirements for the code plus and additional note what the code should have. Goals Understand the ASCII representation of character values. Understand...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT