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];
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
EtoAC65E1and get ..AC65E1again. Presumably you meantAC65EF.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
strtolpassing16as thebaseparameter, orfscanfto read into an integer in the first place.