Reputation: 133
For example, when
val = 0x505050
val1 = 0x005000
The result should be the addtion of the two hexadecimals, which is:
0x50a050
What I did is:
int hex;
int channel;
int result;
printf("first hex number: ");
scanf("%x", &hex);
printf("second hex number: ");
scanf("%x", &channel);
result = hex + channel;
printf("Your new hex value is: ");
printf("0x%x\n", result);
And this brought me the right result. However, the problem here is that, when the second (channel) value is like,
50 00 00
it is base 10, not 16 so the addition of 0x505050 and 50 00 00 should be 0x825050 but I get 0x5050a0.
How can I manage this problem? Also, when the second input is not hex and it is negative, for example,
-50 -40 -30
I get 0x505000, but it should be 0x1e2832
How can handle with negative numbers?
Can someone please help me out with this problem?
Upvotes: 0
Views: 325
Reputation: 64730
You will have to gather your input as text, and then go through logic of your own creation to figure out if the text should be interpreted as base16, base10, or something else:
char input[1024];
fgets(input, 1024, stdin);
// Examine input to figure out if base16, or base10.
500000(base10) + 0x505050(base16)
should be 0x825050(base16)
. But when I add those together, I get 0x57F170(base16)
.Upvotes: 1