0

I have a C code during which I open a file to input a hexadecimal number. Then I want to add the number obtained it to another single digit hexadecimal and finally display the number in hex. e.g. I input AC65E1 and I add E to it and display AC65EF.

Is there a way to do it without going through the conversion to decimal and adding in decimal and then converting back to hexadecimal? If no what is the time-optimum way to do it?

EDIT: I think my question my misinterpreted, I wanted to know if I can use the '+' with Hex? if I couldnt then I would have to use change to decimal to use the '+' operator. Sorry for any mis-typing!

Now I am reading the data from the file into a char hex_num[10];

Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
  • Try this: `printf("%02x %02X %d\n", 42, 42, 42);` :) – pmg Mar 02 '11 at 14:40
  • 1
    Even after your edit, @Syntax_Error, you still exhibit a fundamental misunderstanding of how numbers are handled on computers. What you have is a string of characters that appears -- to the human eye -- to be a number (and it happens to look like hexadecimal notation). The situation would be exactly the same even if the string looked like decimal. You can't perform numeric arithmetic on strings. You need to convert the string to its real numeric type (which might look like decimal or hex when printed, but in reality is binary and _doesn't matter_) to do arithmetic on it. – Lightness Races in Orbit Mar 02 '11 at 15:04
  • possible duplicate of [How can I convert a hexadecimal number to base 10 efficiently in C?](http://stackoverflow.com/questions/10324/how-can-i-convert-a-hexadecimal-number-to-base-10-efficiently-in-c) – Lightness Races in Orbit Mar 02 '11 at 15:06
  • @Tomalak: yes that is exactly what I am asking! is there an easier way? like is there an atoi for hex? can I do like atoi(hex1) + atoi(hex2)=hex3 and I get hex3? – Syntax_Error Mar 02 '11 at 17:45
  • You're still not getting it! Your input is not "hex"; it is a string. Convert it to a number, specifying that the input is represented in base-16. `atoi` and `strtoul` have parameters for this. The output of arithmetic is not decimal, or hex, or anything else. Which C book are you using? – Lightness Races in Orbit Mar 02 '11 at 17:48

5 Answers5

3

Actually, computers do all their work in binary, so unless you want to go to a great deal of pointless effort to force it to do the arithmetic on the raw hex digits, you'll want to parse the numbers, add them, and render the result back out again as hex.

unsigned int value1, value2;
fscanf(fp, "%x %x", &value1, &value2)

unsigned int result = value1 + value2;
printf("%x\n", result);
David Given
  • 13,277
  • 9
  • 76
  • 123
2

Mathematics in C and other languages are not done in any particular base — except at the lowest level of processing where, of course, they take place in binary.

I think your question is broken because you want to add E to AC65E1 and get .. AC65E1 again. Presumably you meant AC65EF.

I suggest read the data from the file and parse strings that look like numbers into integers, perform your arithmetic, then output them again in whatever representation you wish.

For this, use strtol passing 16 as the base parameter, or fscanf to read into an integer in the first place.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

is this what you want: http://www.ehow.com/how_7529899_read-hex-file-ansi-c.html

Kevin
  • 24,871
  • 19
  • 102
  • 158
1

You could write a character-by-character adder, the same way you would do this naturally on paper, i.e. compare last digits '1' + 'E' = 'F', no carry, etc.

That may not be significantly more efficient, though, assuming your code to parse in a hexadecimal number is efficient (it's a lot easier to do than decimal) and assuming your numbers are small enough to fit into a machine register or two - if you had 1000+ digit hex strings then the character-by-character way may be simplest.

And in any case these are read from a file: the IO will be much slower than the addition. So I wouldn't worry about every last bit of performance for the add.

Rup
  • 33,765
  • 9
  • 83
  • 112
0

You seem a bit confused.

If you do:

int a = 43, b = 12;

printf("%d+%d=%d\n", a, b, a + b);

the computer is not doing any "adding in decimal". The addition is done in binary, which is converted to/from decimal text at compile-time for the literals, and at run-time for the printout.

You should simply use strtol to parse the numbers, or sscanf(), and add.

Manually writing text-based routines to "add in hexadecimal" will not be better in any way.

unwind
  • 391,730
  • 64
  • 469
  • 606