Skip to content

Instantly share code, notes, and snippets.

@essare
Created December 11, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save essare/6a26db550f7680476f984835b19325ec to your computer and use it in GitHub Desktop.
Save essare/6a26db550f7680476f984835b19325ec to your computer and use it in GitHub Desktop.
#include "Conversion.h"
/**
* @Author: ESSARE AYOUB
* Conversion Class
* Convert from Bin to Dec - Oct - Hex
*/
void Conversion::main(std::vector<std::wstring> &args)
{
// TODO Auto-generated method stub
int input = 0;
std::wcout << std::wstring(L" ||Chose an Operation:") << std::endl;
std::wcout << std::wstring(L" ||==> 1) Octal To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 2) Decimal To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 3) Hexa To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 4) From Binary To all") << std::endl;
std::wcout << std::wstring(L" Your Choice: ");
Scanner *scn = new Scanner(System::in);
try
{
input = scn->nextInt();
if (input == 1)
{
std::wcout << std::wstring(L"+============================+") << std::endl;
std::wcout << std::wstring(L"| Octal To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+============================+") << std::endl;
// Analyzing Input
OctToBinInput();
delete scn;
}
else if (input == 2)
{
std::wcout << std::wstring(L"+============================+") << std::endl;
std::wcout << std::wstring(L"| decimal To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+============================+") << std::endl;
// Analyzing Input
DecToBinInput();
delete scn;
}
else if (input == 3)
{
std::wcout << std::wstring(L"+==========================+") << std::endl;
std::wcout << std::wstring(L"| Hexa To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+==========================+") << std::endl;
// Analyzing Input
HexToBinInput();
delete scn;
}
else if (input == 4)
{
std::wcout << std::wstring(L"+=====================+") << std::endl;
std::wcout << std::wstring(L"| From Binary To All |") << std::endl;
std::wcout << std::wstring(L"+=====================+") << std::endl;
// Analyzing Input
FromBinToAllInput();
delete scn;
}
else
{
std::wcout << std::wstring(L"Invalid number! Please try Again") << std::endl;
AnotherOperation();
}
}
catch (const InputMismatchException &e)
{
std::wcout << std::wstring(L"Invalid number! Please try Again") << std::endl;
AnotherOperation();
}
}
void Conversion::OctToBinInput()
{
Scanner *input = new Scanner(System::in);
std::wcout << std::wstring(L"Enter octal number: ");
std::wstring oct = input->next();
OctalToBin(oct);
delete input;
}
void Conversion::DecToBinInput()
{
Scanner *input = new Scanner(System::in);
std::wcout << std::wstring(L"Enter Decimal number: ");
std::wstring dec = input->next();
DecimalToBin(dec);
delete input;
}
void Conversion::HexToBinInput()
{
Scanner *input = new Scanner(System::in);
std::wcout << std::wstring(L"Enter Hexa number: ");
std::wstring hex = input->next();
HexaToBin(hex);
delete input;
}
void Conversion::FromBinToAllInput()
{
Scanner *input = new Scanner(System::in);
std::wcout << std::wstring(L"Enter Binary number: ");
std::wstring bin = input->next();
FromBinToAll(bin);
delete input;
}
int Conversion::OctalToBin(const std::wstring &octalNumber)
{
std::wstring binary = L"";
for (int j = 0; j < octalNumber.length(); j++)
{
wchar_t num = octalNumber[j];
num -= L'0';
if (num < 0 || num>7)
{
std::wcout << std::wstring(L"Invalid octal number! (<7 digits) Please try again.") << std::endl;
OctToBinInput();
return -1;
}
try
{
binary = Integer::toBinaryString(std::stoi(octalNumber));
}
catch (const NumberFormatException &e)
{
std::wcout << std::wstring(L"Number format is wrong! Please try again.") << std::endl;
OctToBinInput();
}
}
std::wcout << std::wstring(L" Binary of ") << octalNumber << std::wstring(L" is: ") << binary << std::endl;
BinToOct(binary);
BinToDec(binary);
BinToHex(binary);
return 1;
}
int Conversion::DecimalToBin(const std::wstring &decimalNumber)
{
std::wstring binary = L"";
for (int j = 0; j < decimalNumber.length(); j++)
{
wchar_t num = decimalNumber[j];
num -= L'0';
if (num < 0 || num>9)
{
std::wcout << std::wstring(L"Invalid Decimal number! Please try again.") << std::endl;
DecToBinInput();
return -1;
}
try
{
binary = Integer::toBinaryString(std::stoi(decimalNumber));
}
catch (const NumberFormatException &e)
{
std::wcout << std::wstring(L"Number format is wrong! Please try again.") << std::endl;
DecToBinInput();
}
}
std::wcout << std::wstring(L" Binary of ") << decimalNumber << std::wstring(L" is: ") << binary << std::endl;
BinToDec(binary);
BinToOct(binary);
BinToHex(binary);
return 1;
}
int Conversion::HexaToBin(const std::wstring &hexaNumber)
{
std::wstring binary = L"";
for (int j = 0; j < hexaNumber.length(); j++)
{
// char num = hexaNumber.charAt(j);
//num -= '0';
if (isHexadecimal(hexaNumber))
{
int i = Integer::valueOf(hexaNumber, 16);
binary = Integer::toBinaryString(i);
}
else
{
std::wcout << std::wstring(L"Invalid Hexa number! Please try again.") << std::endl;
HexToBinInput();
return -1;
}
}
std::wcout << std::wstring(L" Binary of ") << hexaNumber << std::wstring(L" is: ") << binary << std::endl;
BinToDec(binary);
BinToOct(binary);
BinToHex(binary);
return 1;
}
bool Conversion::isHexadecimal(const std::wstring &text)
{
text = boost::trim_copy(text);
std::vector<wchar_t> hexDigits = {L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L'a', L'b', L'c', L'd', L'e', L'f', L'A', L'B', L'C', L'D', L'E', L'F'};
int hexDigitsCount = 0;
for (auto symbol : text.toCharArray())
{
for (auto hexDigit : hexDigits)
{
if (symbol == hexDigit)
{
hexDigitsCount++;
break;
}
}
}
return true ? hexDigitsCount == text.length() : false;
}
long long Conversion::BinToDec(const std::wstring &bin)
{
long long decimalValue = 0;
decimalValue = long long::valueOf(bin, 2);
std::wcout << std::wstring(L"+=====================+") << std::endl;
//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
std::wcout << std::wstring(L"| Decimal: ") << Long::toString(decimalValue) << std::endl;
// System.out.println("+=====================+");
return decimalValue;
}
long long Conversion::BinToOct(const std::wstring &bin)
{
long long octalValue = 0;
octalValue = long long::valueOf(bin, 2);
std::wcout << std::wstring(L"+=====================+") << std::endl;
std::wcout << std::wstring(L"| Octal: ") << Long::toOctalString(octalValue) << std::endl;
// System.out.println("+=====================+");
return octalValue;
}
long long Conversion::BinToHex(const std::wstring &bin)
{
int hex = 0;
int decimal = Integer::valueOf(bin,2);
//JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString':
std::wstring hexStr = Integer::toString(decimal,16);
std::wcout << std::wstring(L"+=====================+") << std::endl;
std::wcout << std::wstring(L"| Hex: ") << (hexStr) << std::endl;
std::wcout << std::wstring(L"+=====================+") << std::endl;
AskForAnotherOper();
return hex;
}
void Conversion::AnotherOperation()
{
int input = 0;
std::wcout << std::wstring(L"\n ||Chose an Operation:") << std::endl;
std::wcout << std::wstring(L" ||==> 1) Octal To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 2) Decimal To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 3) Hexa To Binary") << std::endl;
std::wcout << std::wstring(L" ||==> 4) From Binary To all") << std::endl;
std::wcout << std::wstring(L" Your Choice: ");
Scanner *scn = new Scanner(System::in);
try
{
input = scn->nextInt();
if (input == 1)
{
std::wcout << std::wstring(L"+============================+") << std::endl;
std::wcout << std::wstring(L"| Octal To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+============================+") << std::endl;
// Analyzing Input
OctToBinInput();
delete scn;
}
else if (input == 2)
{
std::wcout << std::wstring(L"+============================+") << std::endl;
std::wcout << std::wstring(L"| decimal To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+============================+") << std::endl;
// Analyzing Input
DecToBinInput();
delete scn;
}
else if (input == 3)
{
std::wcout << std::wstring(L"+==========================+") << std::endl;
std::wcout << std::wstring(L"| Hexa To Binary Operation |") << std::endl;
std::wcout << std::wstring(L"+==========================+") << std::endl;
// Analyzing Input
HexToBinInput();
delete scn;
}
else if (input == 4)
{
std::wcout << std::wstring(L"+=====================+") << std::endl;
std::wcout << std::wstring(L"| From Binary To All |") << std::endl;
std::wcout << std::wstring(L"+=====================+") << std::endl;
// Analyzing Input
FromBinToAllInput();
delete scn;
}
else
{
std::wcout << std::wstring(L"Invalid number! Please try Again") << std::endl;
AnotherOperation();
}
}
catch (const InputMismatchException &e)
{
std::wcout << std::wstring(L"Invalid number! Please try Again") << std::endl;
AnotherOperation();
}
}
void Conversion::AskForAnotherOper()
{
std::wcout << std::wstring(L"Calculate Another Operation [Y/n] ?") << std::endl;
Scanner *anotherInputScan = new Scanner(System::in);
std::wstring anotherInput = anotherInputScan->next();
if (anotherInput == L"Y" || anotherInput == L"y" || anotherInput == L"Yes" || anotherInput == L"YES" || anotherInput == L"yes")
{
AnotherOperation();
}
else if (anotherInput == L"n" || anotherInput == L"N" || anotherInput == L"no" || anotherInput == L"NO")
{
std::wcout << std::wstring(L"All Done! Thank you for using me :) ") << std::endl;
}
else
{
std::wcout << std::wstring(L"Invalid response! Please try again.") << std::endl;
AskForAnotherOper();
}
delete anotherInputScan;
}
void Conversion::FromBinToAll(const std::wstring &bin)
{
try
{
BinToDec(bin);
BinToOct(bin);
BinToHex(bin);
}
catch (const NumberFormatException &e)
{
std::wcout << std::wstring(L"Invalid Binary number! Please try again.") << std::endl;
FromBinToAllInput();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment