In: Computer Science
int bintodec(string); // converts a binary number (represented
as a STRING) to decimal
int hextodec(string); // converts a hexadecimal number (represented
as a STRING) to decimal
string dectobin(int); // converts a decimal number to binary
(represted as a STRING)
string dectohex(int); // converts a decimal number to hexadecimal
(represted as a STRING)
//the addbin and addhex functions work on UNsigned numbers
string addbin(string, string); // adds two binary numbers
together (represented as STRINGS)
string addhex(string, string); // adds two hexadecimal numbers
together (represented as STRINGS)
int main()
{
cout<<"SIGNED binary 10011 = SIGNED decimal
"<<bintodec("10011")<<endl;
cout<<"SIGNED binary 01110 = SIGNED decimal
"<<bintodec("01110")<<endl<<endl;
cout<<"SIGNED hexadecimal A1 = SIGNED decimal
"<<hextodec("A1")<<endl;
cout<<"SIGNED hexadecimal 3C = SIGNED decimal
"<<hextodec("3C")<<endl<<endl;
cout<<"SIGNED decimal 25 = SIGNED binary
"<<dectobin(25)<<endl;
cout<<"SIGNED decimal -18 = SIGNED binary
"<<dectobin(-18)<<endl<<endl;
cout<<"SIGNED decimal 28 = SIGNED hexadecimal
"<<dectohex(28)<<endl;
cout<<"SIGNED decimal -15 = SIGNED hexadecimal
"<<dectohex(-15)<<endl<<endl;
cout<<"binary 1101 + 1000 =
"<<addbin("1101", "1000")<<endl;
cout<<"binary 11000 + 1011 =
"<<addbin("11000", "1011")<<endl<<endl;
cout<<"hexadecimal A4 + A5 =
"<<addhex("A4", "A5")<<endl;
cout<<"hexadecimal 2B + C =
"<<addhex("2B", "C")<<endl<<endl;
return 0;
}
int bintodec(string bin)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
int hextodec(string hex)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
string dectobin (int dec)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
string dectohex(int dec)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
string addbin(string bin1, string bin2)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
string addhex(string hex1, string hex2)
{
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
}
int bintodec(string bin)
{
int decimal, base, remainder;
decimal = 0;
base = 1;
int x = atoi(bin.c_str()); //String to integer conversion
while (x > 0)
{
remainder = x % 10;
decimal = decimal + remainder * base;
base = base * 2;
x = x / 10;
}
return x;
}
int hextodec(string hex)
{
char arr[100];
int len, i, test, length, decimal, base = 16;
i = 0;
strcpy(arr, hex.c_str()); //string to character array
len = strlen(hex);
len = len - 1;
length = len;
while (len > 0)
{
test = arr[i];
if (test >= 48 && test <= 57) // test Between 0-9
{
decimal += (test - 48) * pow(base, length - i - 1);
}
else if ((test >= 65 && test <= 70)) // test Between A-F
{
decimal += (test - 55) * pow(base, length - i - 1);
}
else if (test >= 97 && test <= 102) // test Between a-f
{
decimal += (test - 87) * pow(base, length - i - 1);
}
else
{ cout << "Not Hex value"; }
len = len -1;
i++ ;
}
return decimal;
}
string dectobin(int dec)
{
int remainder;
if (dec <= 1)
{
cout << dec;
return;
}
remainder = dec % 2;
dectobin(dec / 2);
cout << remainder;
}
string dectohex(int dec) //use _itoa(), if itoa() does not work;
{
char buffer[50];
itoa(dec, buffer, 16);
cout << buffer;
}
string addbin(string bin1, string bin2)
{
int num1, num2, result;
num1 = bintodec(bin1);
num2 = bintodec(bin2);
result = num1 + num2;
dectobin(result);
}
string addhex(string hex1, string hex2)
{
int num1, num2, result;
num1 = hextodec(hex1);
num2 = hextodec(hex2);
result = num1 + num2;
dectohex(result);
}