0% found this document useful (0 votes)
7 views4 pages

Coversion (Binary)

The document contains C++ code for converting decimal numbers to binary and vice versa, as well as converting decimal numbers to hexadecimal. It defines a Converter class with methods for these conversions and includes a main function that provides a menu for user interaction. The code handles both integer and fractional parts for binary conversion and includes error handling for invalid binary digits.

Uploaded by

manliclicmax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Coversion (Binary)

The document contains C++ code for converting decimal numbers to binary and vice versa, as well as converting decimal numbers to hexadecimal. It defines a Converter class with methods for these conversions and includes a main function that provides a menu for user interaction. The code handles both integer and fractional parts for binary conversion and includes error handling for invalid binary digits.

Uploaded by

manliclicmax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//Decimal to Binary, Binary to Decimal

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

class Converter {
public:
//convert decimal
string toBinary (double decimal){
if (decimal < 0) {
return "-" + toBinary (-decimal);
}
int intPart = static_cast<int>(decimal);
double fracPart = decimal - intPart;

//(integer)
string binaryInt = "";
if (intPart == 0) binaryInt = "0";
while (intPart > 0) {
binaryInt += (intPart % 2) ? '1' : '0';
intPart /= 2;
}
reverse(binaryInt.begin(), binaryInt.end());

//(fractional values)
string binaryFrac = "";
int limit = 20; // max number of digits in fractional binary
while (fracPart > 0 && binaryFrac.length() < limit) {
fracPart *= 2;
if (fracPart >= 1.0) {
binaryFrac += "1";
fracPart -= 1.0;
} else {
binaryFrac += "0";
}
}

return binaryFrac.empty() ? binaryInt : binaryInt + "." + binaryFrac;


}

//convert binary
double toDecimal(const string & binary) {
size_t point = binary.find('.');
double decimal = 0.0;
//(integer)
int power = 0;
int i = (point == string::npos) ? binary.length() - 1 : point - 1;
for (int j = i; j >= 0; --j) {
char c = binary[j];
if (c == '1') {
decimal += pow(2, power);
} else if (c != '0') {
throw invalid_argument("Invalid binary digit");
}
power++;
}

//(fractional values)
if (point != string::npos) {
double frac = 0.0;
double weight = 0.5;
for (size_t k = point + 1; k < binary.length(); ++k) {
char c = binary[k];
if (c == '1') {
frac += weight;
} else if (c != '0') {
throw invalid_argument("Invalid binary digit.");
}
weight /= 2.0;
}
decimal += frac;
}

return decimal;
}
};

int main() {
Converter converter;
int choice;
cout << "Conversion Menu:\n";
cout << "1. Decimal to Binary\n";
cout << "2. Binary to Decimal\n";
cout << "Enter your choice (1 or 2): ";
cin >> choice;

if (choice == 1) {
double decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
cout << "Binary: " << converter.toBinary(decimal) << endl;
} else if (choice == 2) {
string binary;
cout << "Enter a binary number: ";
cin >> binary;
try {
double result = converter.toDecimal(binary);
cout << "Decimal: " << result << endl;
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
} else {
cout << "Invalid choice.\n";
}
return 0;
}

Decimal To Hexa
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string toHex(unsigned long long decimal) {


if (decimal == 0) return "0";

string hex = "";

while (decimal > 0) {


int remainder = decimal % 16;
char hexDigit;

switch (remainder) {
case 10: hexDigit = 'A'; break;
case 11: hexDigit = 'B'; break;
case 12: hexDigit = 'C'; break;
case 13: hexDigit = 'D'; break;
case 14: hexDigit = 'E'; break;
case 15: hexDigit = 'F'; break;
default: hexDigit = remainder + '0'; // for 0–9
}

hex += hexDigit;
decimal /= 16;
}
reverse(hex.begin(), hex.end());
return hex;
}

int main() {
unsigned long long decimal;

cout << "Enter a decimal number: ";


cin >> decimal;

string hex = toHex(decimal);


cout << "Hexadecimal: " << hex << endl;

return 0;
}

You might also like