Archived

This topic is now archived and is closed to further replies.

Adding hexadecimals

This topic is 5893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have these 12 hexadecimals : 30, 32, 30,31,30,30,30,30,30,30,30,38. I add these together in a for loop into a unsigned char(because i do not want the carries) like this: unsigned char buffer[12]; buffer[0] = ''\x30'' etc etc unsigned char test; for(loop=0; loop<12; loop++) { test += buffer[loop] } i know the outcome must be 0B in hexadecimal but i get 4b. What am i doing wrong? Thanx in advance

Share this post


Link to post
Share on other sites
The unsigned char is only an 8-bit value; maximum ranges that can be stored in such a variable are therefore 0 - 255. Because your summation exceeds the upper boundaries of the variable, it ''rolls over'' ( think of the odometer in a car rolling over from 99,999 -> 0 .)

Simply change test to an int.

Share this post


Link to post
Share on other sites