1

I want to add two hexadecimals values to get a resultant hexadecimal value i have written the following code however when i print the value of resultant the resultant value is write for example "abc" ->61+62+63=186 however on writing jk ->6a+6b i should get something like d5 however it gives 12.

this is tthe code i write:

i have also defined the globals as

  char buffer[20];
  long int li ;

  for (int i = 0; i < length; ++i) {
      itoa(TextAlia[i], buffer, 16);
      li = li + atol (buffer);
  }
rubber boots
  • 14,924
  • 5
  • 33
  • 44
Emma Rochweel
  • 87
  • 1
  • 13
  • 1
    The representations `6a` (hexadecimal) `152` (octal), `1101010` (binary) and `106` (decimal) are just notations that need their proper context for operations. Normally, standard math functions only work on **decimal** representations, so you'll need to convert your **number representation** into decimal (and back). – rubber boots Jul 18 '12 at 14:04

3 Answers3

3
li = li + atol (buffer);

atol stops at the first non-digit (decimal). To parse hexadecimal representations, use

li += strtol(buffer, NULL, 16);
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
1

I'll assume here that you have two strings "TextAlia" and "TextAlib," each of which contain a string representation of a hexidecimal number. One typical way to add the numbers and produce a hexidecimal string as output:

int a, b;
sscanf(TextAlia, "%x", &a);
sscanf(TextAlib, "%x", &b);
printf("%x", a + b);

This however is not the fastest possible way to add hexadecimal numbers in C, and is not written in typical C++ style either.

0

Try looking at this: Adding hexa values in C#

Or try this:

int value = Convert.ToInt32(hexString1, 16) + Convert.ToInt32(hexString2, 16);

Hope this helps.

Community
  • 1
  • 1
plast1K
  • 469
  • 3
  • 13