 # Converting 8-bit Binary Integer to Its Decimal Using C++

Subject Computer Science C-Family Programming

## Question

Write a C++ program converting an 8-bit binary integer to its decimal and converting a decimal integer to its 8-bit binaries. You can create functions declared as
// ============================================================================
// Function: BinToDec
// Description: This function converts an 8-bit binary integer to its decimal
// Parameter:   s [IN] -- a C-string with 8-bit binary digits received
// Return:      A decimal integer value calculated from s
// ============================================================================

int BinToDec(char* s);

// ============================================================================
// Function: DecToBin
// Description: This function converts a decimal integer to its binary bits
// Parameter:   n [IN] -- a decimal integer received, less than 256
// Return:      A C-string with 8-bit binary digits calculated from n
// ============================================================================

char* DecToBin(int n);
The purpose is to make you understand the number system concept. So you have to manually implement the ideas without making use of some library function, like itoa(), atoi(), pow(), etc. that make the assignment meaningless. Only function allowed to use is strlen().
• For BinToDec(),
1. Multiplication complexity of O(n) is required
2. Using Horner's method is suggested
• For DecToBin(),
1. Correctly using memory to return char* is required
2. The method of dividing by 2 and taking remainders reversely is suggested
Then you call two functions in main() like this:
while (...)
{
... ...
switch (n)
{
case 1: // How to call BinToDec()
{
// Call BinToDec()
// Output result
}
case 2: // How to call DecToBin()
{
// Call DecToBin()
// Output result
}
default: // Check others
{
... ...
}
}
}
Your program should be run like the following:
>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------
Please Enter 8-bit binary digits (e.g., 11110000): 10001001
The decimal integer of 10001001b is 137d

>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------
Please Enter a decimal integer less than 256: 137
The binary of 137d is 10001001b

>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------
Bye.
Press any key to continue . .

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <iostream>

char* DecToBin(int n);
int BinToDec(char* s);

using namespace std;

int main()
{
int choice, val, decConvert;
char *binary = new char;
char *binConvert;
while (true)
{
cout << ">>> Please select the conversion type:" << endl;
cout << "1. Binary to Decimal:" << endl;
cout << "2. Decimal to Binary" << endl;
cout << "3. Exit" << endl;
cout << "--------------------------------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: // How to call BinToDec()
{
cout << "Please Enter 8-bit binary digits (e.g., 11110000): ";
do{
binary = getchar();
}
while(binary != '0' && binary != '1');
binary = '\0';
for(int i = 1; i < 8; i++)
binary[i] = getchar();
decConvert = BinToDec(binary);
cout << "The decimal integer of " << binary << "b is " << decConvert << "d" << endl;
break;
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

## Related Homework Solutions

Data Processing in C++ \$25.00
Computer Science
Data Processing
C++ Programming
Reservoir Levels
Filters
Conditions
Dates
TSV
Excel
Stream
Objects
Variables
Image Editing in C++ \$75.00
Computer Science
C++ Programming
Image Editing
Binary Files
Destinations
Grayscale
Bitmaps
Pixels
Boundaries
Loops
Patterns
Instructions
C Programming Problems \$13.00
Computer Science
C Programming
Variables
Pointers
Compilation
Integers
Form Specifiers
Characters
The Game Of Life: Multithreaded Version \$128.00
Computer Science
Game of Life
C Programming
Arguments
Conditions
Loops
Rows
Data
Maps
Functions
Results
Recursive Multiplication in C++ \$15.00
Computer Science
C++ Programming
Recursive Multiplication
Arguments
Functions
Statements
Variables
C Programming Problems \$13.50
Computer Science
C Programming