Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Science: Assembly Language for x86 Processors Conversion_between_Binary

ID: 3871194 • Letter: C

Question

Computer Science: Assembly Language for x86 Processors

Conversion_between_Binary_and_Decimal

I have to make two functions, int BinToDec(char* s) and char* DecToBin(int n).

Write a C++ program converting an 8-bit unsigned binary to its decimal and converting a positive decimal 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 (...)

{

// Show menu and read case action

... ...

switch (n)

{

case 1: // How to call BinToDec()

{

// Prompt and Read input

// Call BinToDec()

// Output result

}

case 2: // How to call DecToBin()

{

// Prompt and Read input

// Call DecToBin()

// Output result

}

default: // Check others

{

... ...

}

}

}

Also, don't use OOP based class/object feature, either a standard string or your created one. You must use C-string.

Explanation / Answer

#include <iostream>
#include <bitset>

int BinToDec(char* x) // Function converting binary to decimal
{
const std::string s = x;
unsigned long value = std::bitset<8>(s).to_ullong();
std::cout << value << std::endl;
}
char* DecToBin(int x) // Function converting Decimal to binary
{
std::bitset<8> bin_x(x);
std::cout << bin_x;
}
int main()
{
int ch,dec;
char bina[9];
  
std::cout << "1. BinToDec 2. DecToBin " ;
std::cin >> ch;
switch(ch)
{
case 1:
std::cout << "Enter the binary value in 8 bits";
std::cin >> bina;
BinToDec(bina);
break;
case 2:
std::cout << "Enter the decimal value";
std::cin >> dec;
DecToBin(dec);
break;
case 3:
std::cout << "Invalid choice . Exiting..";
}

return 0;
}

This is a simple program converting decimal to binary and vice versa

Reach out to me if any queries :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote