Let me start off by saying that this is homework, and I have most of it completed. However, I am stuck on how to convert decimal to hexadecimal. The assignment is to add two hexadecimal numbers and output the answer. If the length of the answer is greater than 10 digits, then output "Addition Overflow." I've completed the conversion of the two strings to decimal and added them together, now I think that I need to convert the answer back to a string. I know it's long, but if you would take the time to read my code, then it would be greatly appreciated. Here's my code:
// hex addition.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void convertToDec(string, string);
void backToHex(double);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> input2;
if (input.length() > 10)
{
cout << "Invalid input" << endl;
system("pause");
return 0;
}
convertToDec(input, input2);
cout << "Would you like to continue? (y/n)";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
decision = false;
system("pause");
return 0;
}
if (choice == 'y' || choice == 'Y')
{
decision = true;
system("pause");
system("cls");
}
} while (decision = true);
system("pause");
return 0;
}
//used to convert the two inputs to decimal
void convertToDec( string input, string input2 )
{
double convert[10] = {0.0};
double convert2[10] = {0.0};
double result;
double result2;
double answer;
double digit1 = 1.0;
double digit2 = 16.0;
double digit3 = 16 * 16;
double digit4 = 16 * 16 * 16;
double digit5 = 16 * 16 * 16 * 16;
double digit6 = 16 * 16 * 16 * 16 * 16;
double digit7 = 16 * 16 * 16 * 16 * 16 * 16;
double digit8 = 16 * 16 * 16 * 16 * 16 * 16 * 16;
double digit9 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;
double digit10 = 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16;
//reverses the inputted string
//converts A, B, C, D, E, and F to number values
for (int i = 0; i < input.length(); i++)
{
convert[i] = input[i];
if(input[i] == 'A')
convert[i] = 10;
if(input[i] == 'B')
convert[i] = 11;
if(input[i] == 'C')
convert[i] = 12;
if(input[i] == 'D')
convert[i] = 13;
if(input[i] == 'E')
convert[i] = 14;
if(input[i] == 'F')
convert[i] = 15;
}
if (input.length() == 1)
result = convert[0] * digit1;
else if (input.length() == 2)
result = (convert[0] * digit2) + (convert[1] * digit1);
else if (input.length() == 3)
result = (convert[0] * digit3) + (convert[1] * digit2) + (convert[2] * digit1);
else if (input.length() == 4)
result = (convert[0] * digit4) + (convert[1] * digit3) + (convert[2] * digit2) + (convert[3] * digit1);
else if (input.length() == 5)
result = (convert[0] * digit5) + (convert[1] * digit4) + (convert[2] * digit3) + (convert[3] * digit2) + (convert[4] * digit1);
else if (input.length() == 6)
result = (convert[0] * digit6) + (convert[1] * digit5) + (convert[2] * digit4) + (convert[3] * digit3) + (convert[4] * digit2) + (convert[5] * digit1);
else if (input.length() == 7)
result = (convert[0] * digit7) + (convert[1] * digit5) + (convert[2] * digit5) + (convert[3] * digit4) + (convert[4] * digit3) + (convert[5] * digit2) + (convert[6] * digit1);
else if (input.length() == 8)
result = (convert[0] * digit8) + (convert[1] * digit7) + (convert[2]) + (convert[3] * digit5) + (convert[4] * digit4) + (convert[5] * digit3) + (convert[6] * digit2) + (convert[7] * digit1);
else if (input.length() == 9)
result = (convert[0] * digit9) + (convert[1] * digit8) + (convert[2] * digit7) + (convert[3] * digit6) + (convert[4] * digit5) + (convert[5] * digit4) + (convert[6] * digit3) + (convert[7] * digit2) + (convert[8] * digit1);
else if (input.length() == 10)
result = (convert[0] * digit10) + (convert[1] * digit9) + (convert[2] * digit8) + (convert[3] * digit7) + (convert[4] * digit6) + (convert[5] * digit5) + (convert[6] * digit4) + (convert[7] * digit3) + (convert[8] * digit2) + (convert[9] * digit1);
//used to test dec results: cout << fixed << result << endl;
//converts for second input
for (int i = 0; i < input2.length(); i++)
{
convert2[i] = input2[i];
if(input2[i] == 'A')
convert2[i] = 10;
if(input2[i] == 'B')
convert2[i] = 11;
if(input2[i] == 'C')
convert2[i] = 12;
if(input2[i] == 'D')
convert2[i] = 13;
if(input2[i] == 'E')
convert2[i] = 14;
if(input2[i] == 'F')
convert2[i] = 15;
}
if (input2.length() == 1)
result2 = convert2[0] * digit1;
else if (input2.length() == 2)
result2 = (convert2[0] * digit2) + (convert2[1] * digit1);
else if (input2.length() == 3)
result2 = (convert2[0] * digit3) + (convert2[1] * digit2) + (convert2[2] * digit1);
else if (input2.length() == 4)
result2 = (convert2[0] * digit4) + (convert2[1] * digit3) + (convert2[2] * digit2) + (convert2[3] * digit1);
else if (input2.length() == 5)
result2 = (convert2[0] * digit5) + (convert2[1] * digit4) + (convert2[2] * digit3) + (convert2[3] * digit2) + (convert2[4] * digit1);
else if (input2.length() == 6)
result2 = (convert2[0] * digit6) + (convert2[1] * digit5) + (convert2[2] * digit4) + (convert2[3] * digit3) + (convert2[4] * digit2) + (convert2[5] * digit1);
else if (input2.length() == 7)
result2 = (convert2[0] * digit7) + (convert2[1] * digit5) + (convert2[2] * digit5) + (convert2[3] * digit4) + (convert2[4] * digit3) + (convert2[5] * digit2) + (convert2[6] * digit1);
else if (input2.length() == 8)
result2 = (convert2[0] * digit8) + (convert2[1] * digit7) + (convert2[2]) + (convert2[3] * digit5) + (convert2[4] * digit4) + (convert2[5] * digit3) + (convert2[6] * digit2) + (convert2[7] * digit1);
else if (input2.length() == 9)
result2 = (convert2[0] * digit9) + (convert2[1] * digit8) + (convert2[2] * digit7) + (convert2[3] * digit6) + (convert2[4] * digit5) + (convert2[5] * digit4) + (convert2[6] * digit3) + (convert2[7] * digit2) + (convert2[8] * digit1);
else if (input2.length() == 10)
result2 = (convert2[0] * digit10) + (convert2[1] * digit9) + (convert2[2] * digit8) + (convert2[3] * digit7) + (convert2[4] * digit6) + (convert2[5] * digit5) + (convert2[6] * digit4) + (convert2[7] * digit3) + (convert2[8] * digit2) + (convert2[9] * digit1);
// used to test dec result: cout << fixed << result2 << endl;
answer = result + result2;
backToHex(answer);
}
//converts answer in decimal to hex
void backToHex( double answer )
{
char hex[10] = {0};
//convert here
}
I'm not sure how to handle the conversion. Like I said, I think that I have to convert the "double answer" to a string, but I'm not sure how I would get the answer to base 16. I was thinking that it may be easier to convert the answer to binary and then to hex, but I'm not quite sure how to do that either. Any help would be greatly appreciated. Thank you for your time!
Are you allowed to use standard C functions? sprintf() can do all that for you, just a matter of giving it the correct forma spcification. E.g. "%x" converts to hex and "%f" to float
Are you allowed to use standard C functions? sprintf() can do all that for you, just a matter of giving it the correct forma spcification. E.g. "%x" converts to hex and "%f" to float
Unfortunately, no. I need to write my own function to do it
Here's the function that I've made to do this, however, it's not perfect. It only works correctly for single digit numbers. Double digits and up are problematic.
//converts answer in decimal to hex
void backToHex( double answer )
{
int remainder;
int a = answer;
int b = a / 16;
char hex[11];
for (int k = 1; k < 10; k++)
{
remainder = a % 16;
if (remainder == 10)
hex[k] = 'A';
else if (remainder == 11)
hex[k] = 'B';
else if (remainder == 12)
hex[k] = 'C';
else if (remainder == 13)
hex[k] = 'D';
else if (remainder == 14)
hex[k] = 'E';
else if (remainder == 15)
hex[k] = 'F';
hex[0] = b;
if (b == 10)
hex[0] = 'A';
else if (b == 11)
hex[0] = 'B';
else if (b == 12)
hex[0] = 'C';
else if (b == 13)
hex[0] = 'D';
else if (b == 14)
hex[0] = 'E';
else if (b == 15)
hex[0] = 'F';
}
if (hex[11] > 0)
{
cout << "----------Addition Overflow----------" << endl;
}
else
cout << hex[0] << hex[1] << endl;
}
If I entered AB for the first input, my output would be: D, but it should be A. Why is that? Is it because "answer" was a double and I changed it to an int? And I doubt that this will handle multiple digits. Any help would be greatly appreciated.
Here's the function that I've made to do this, however, it's not perfect. It only works correctly for single digit numbers. Double digits and up are problematic.
If I entered AB for the first input, my output would be: D, but it should be A. Why is that? Is it because "answer" was a double and I changed it to an int? And I doubt that this will handle multiple digits. Any help would be greatly appreciated.//converts answer in decimal to hex void backToHex( double answer ) { int remainder; int a = answer; int b = a / 16; char hex[11]; for (int k = 1; k < 10; k++) { remainder = a % 16; if (remainder == 10) hex[k] = 'A'; else if (remainder == 11) hex[k] = 'B'; else if (remainder == 12) hex[k] = 'C'; else if (remainder == 13) hex[k] = 'D'; else if (remainder == 14) hex[k] = 'E'; else if (remainder == 15) hex[k] = 'F'; hex[0] = b; if (b == 10) hex[0] = 'A'; else if (b == 11) hex[0] = 'B'; else if (b == 12) hex[0] = 'C'; else if (b == 13) hex[0] = 'D'; else if (b == 14) hex[0] = 'E'; else if (b == 15) hex[0] = 'F'; } if (hex[11] > 0) { cout << "----------Addition Overflow----------" << endl; } else cout << hex[0] << hex[1] << endl; }
Ouch!
Use a character array for the HEX characters. Useremainder as an index into that array to 'convert' the remainder into a hex digit. Now your 30-line conversion is 2 lines.
That it should make it easier to see what you need to do next.
Ouch! Use a character array for the HEX characters. Use remainder as an index into that array to 'convert' the remainder into a hex digit. Now your 30-line conversion is 2 lines. That it should make it easier to see what you need to do next.
This is what you meant right?
char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int k = 0; remainder < 10; k++)
{
remainder = a % 16;
hex[remainder];
} I know you probably think I'm dumb... But I'm trying to understand. Arrays are very new to me
Close, don't forget 0. It makes all the difference. :) It's also possible to use a trick with strings and avoid typing out the whole array:
char const* hex = "0123456789ABCDEF";
// get the least significant digit in hex
char digit = hex[value % 16];Except what's that loop for?
And what are you doing with hex[remainder];?
I know you probably think I'm dumb... But I'm trying to understand. Arrays are very new to me
No, just inexperienced. We were all there at one time...
Except what's that loop for?
And what are you doing with hex[remainder];?
No, just inexperienced. We were all there at one time...
I don't need the loop. I realize that now, and as for the "hex[remainder]," I changed it to "hex[answer % 16];" which gives me the last digit. I'm not quite sure how to proceed...
Except what's that loop for?
And what are you doing with hex[remainder];?
No, just inexperienced. We were all there at one time...
I figured out how to get the second digit, however, my problem from an earlier post still applies. If I enter "AB," then that in decimal is 171. If the second input is "1," then 1 + 171 would be 172. However, if I divide 172 by 16, then it spits out a "D." Rather than an "A," which it should be. It seems as though it has converted the second input to 49. Is that an error in my convertToDec function? How would I fix it?
Well, you you are going backwards and just broke your solution.
What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?
You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.
Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.
I've fixed the problem that I had earlier by adding another function to do the addition. Adding two digits together words perfectly, unless the answer is more than two digits. I'm not quite sure how to do this for more than one digit. Any suggestions? I can't post code right now, because I'm on a different computer, but I can provide what I've done later if necessary. Thanks!
Well, you you are going backwards and just broke your solution.
What is remainder? What is the result of hex[remainder]? What happens to that result without an = sign somewhere? How does changing [remainder] to [answer % 16] solve your problem?
You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.
Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.
I realize that those things aren't what I need to do. I have an algorithm for what I need to do, but I'm not quite sure how to do it.
-Use an array of 10, with each element of the array representing the number of 16^whatever power's are in the answer
-Index hex array with the values of the above array
That sounds better in my head, I hope it makes sense.
>>Adding two digits together words perfectly, unless the answer is more than two digits. I'm not quite sure how to do this for more than one digit.
When you can't figure something out in your head, go back to pencil and paper. Things are generally easier to figure out that way. Here's one scenario that might work for you, assuming it is modeling the problem I think you are having.
Example 1:
AC + EF = [10][12] //AC as array of int
+ [14][15] //EF as array of int
-------- //add columns
[24][27] //work right to left to get each cell less than 16
[24][16 + 11] //16 with remainder 11, carry 16 as a 1 to next cell to left
[24 + 1][11]
[25][11]
[16 + 9][11] //16 with remainder 9, carry 16 as a 1 to next cell to left
[1][9][11]
19B //convert int values in each cell to hex char
Example 2:
FFF + 11 = [15][15][15]
+ [ 1][ 1]
______________
[15][16][16]
[15][17][ 0]
[16][ 1][ 0]
[ 1][ 0][ 1][ 0]
1010 is hex representation of the sum
In general:
1) convert hex string to int array where each char becomes an int with the same index as the char and has a value that ranges from 0-15. Repeat for the other hex string
2) right justify the int arrays, padding the smaller array to the left if needed
3) add ints with same index to generate a sum and place that sum in the appropriate cell of the resultant int array
4) using the result int array from #3 and working right to left,
5) if int > 15 in this cell subtract 16 from this int
6) the remainder (or the original value if it was less than 16) remains in this cell
7) if the starting int was over 15 then add one to the cell to left of current cell
repeat for cell to left of current one until all cells evaluated
8) once all ints in the array have values under 16, convert each int back into a hex char and place it in the appropriate cell of the result hex stringWell, you you are going backwards and just broke your solution.
What is remainder? What is the result of hex[remainder]? What happens to that result without an = sign somewhere? How does changing [remainder] to [answer % 16] solve your problem?
You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.
Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.
I think I've got it now, except for one part. Here's what I've done:
//converts answer in decimal to hex
void backToHex( double answer )
{
char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int remainder[11] = {0};
if (remainder[11] != 0)
{
cout << "----------Addition Overflow----------" << endl;
}
remainder[0] = answer / 16;
//remainder[1] = not sure what to put here, as I cannot use modulus
remainder[2] = answer / 256; //16 to the 2
remainder[3] = answer / 4096; //16 to the 3
remainder[4] = answer / 65536; //16 to the 4
remainder[5] = answer / 1048576; //16 to the 5
remainder[6] = answer / 16777216; //16 to the 6
remainder[7] = answer / 268435456; //16 to the 7
remainder[8] = answer / 4294967296; //16 to the 8
remainder[9] = answer / 68719476736; //16 to the 9
for (int i = 0; i < 10; i++)
cout << hex[remainder[i]];
} For the second digit in the number, It would be ideal to use modulus, but since my answer is a double, that is not an option. How would you suggest that I go about getting that digit?
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.
OR in pseudocode, maybe somehting like this:
x, y, remainder
remainder = y
while x less than or equal to remainder
remainder = remainder - x>>you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
or just call standard C function fmod()
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y; if x < y what is the remainder if you remove as many x's from y as you can.
OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
Is there an easier approach that I'm not seeing?
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y; if x < y what is the remainder if you remove as many x's from y as you can.
OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
Here's what I've done:
void backToHex( double answer )
{
int a = answer;
char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int remainder[11] = {0};
if (remainder[11] != 0)
{
cout << "----------Addition Overflow----------" << endl;
}
remainder[0] = mod(answer, 16.0);
cout << remainder[0];
}
double mod(double answer, double y)
{
double remainder = y;
while (answer <= remainder)
remainder = remainder - answer;
return remainder;
} However, It always returns 16. Why is that?
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y; if x < y what is the remainder if you remove as many x's from y as you can.
OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y; if x < y what is the remainder if you remove as many x's from y as you can.
OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
Here's what I've done:
void backToHex( double answer )
{
int a = answer;
char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int remainder[11] = {0};
if (remainder[11] != 0)
{
cout << "----------Addition Overflow----------" << endl;
}
remainder[0] = mod(answer, 16.0);
cout << remainder[0];
}
double mod(double answer, double y)
{
double remainder = y;
while (answer <= remainder)
remainder = remainder - answer;
return remainder;
} However, It always returns 16. Why is that?