 # 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

Programming Problem: Calories & Money \$38.00
Computer Science
Codes
C++
Calories
Money
Input
Output
Food
Dynamic Programming
Arrays
Statements
Variables
Integers
Strings
File Management
Recurrence
C entsMemoized Implementation
Iterative Implementation
Algorithms
Celsius to Fahrenheit Table - C++ Solution \$10.00
C++
Celsius
Table
Fahrenheit
Program
Temperature
Pseudocode
Input
Output
Data
Processing
Prompt
Tip Amount Program \$30.00
Computer
Science
C
Language
Tip
Amount
Program
Pseudo
Code
Restaurant
Charge
Taxes
Percentage
Contact List Program in C++ \$18.00
Computer Science
Programming
Contact List
C++
Phone Numbers
Classes
Constructors
Member Variables
Strings
Pointers
Names
People
Test Values
Functions
Print
Search
Delete
C++: Poker Hand \$15.00
C++
Computer
Science
Poker
Straight
Flush
Four
Kind
Full
House
Three
Two
Pairs
High
Card
Do-While Loop For a Ticket-Taker Problem \$15.00
Programming
Computer Science
C++
Ticket-Taker
Prices
Do-While Loop
Discounts
Children
Constant Values
Integers
Sales
Messages
Input
Output
Live Chats