Search code examples
c++wxwidgetshex

Adding Hexadecimal values to get a resultant hexadecimal


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);
  }

Solution

  • li = li + atol (buffer);
    

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

    li += strtol(buffer, NULL, 16);