Skip to main content
2 of 3
+ solution with loop.
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81