This code uses only 8 additional bytes in stack(int i, j
).
int i;
for (i = 0; i < 8; i++) {
array[7 - i] = temperature % 16;
temperature /= 16;
if (temperature == 0)
break;
}
if (i == 8)
i--;
int j;
for (j = 0; j <= i; j++)
array[j] = array[7 - i + j];
for (j = i + 1; j < 8; j++)
array[j] = 0;
for (j = 0; j < 8; j++)
if (array[j] < 10)
array[j] += '0';
else
array[j] += 'a' - 10;
This code first converts temperature = 0x1f12
to array[8] = { 0, 0, 0, 0, 1, 15, 1, 2}
.
Then shifts the elements of array
so that it becomes array[8] = { 1, 15, 1, 2, 0, 0, 0, 0 }
.
And then converts the numbers to corresponding characters: array[8] = { '1', 'f', '1', '2', '0', '0', '0', '0' }
.
Note also that this if condition
if (i == 8)
i--;
is never met, since break condition always suffices in the first for
loop, even for temperature >= 0x10000000. It's just there in the hope that it might help someone understand this code.