Serialization is the process of converting in-memory objects or data structures to a format that can be written to disk or sent over a network (generally an array of bytes).
Deserialization is the reverse process.
It depends on where you consider the iteration to begin. The values of i I posted are taken inside the body of the loop. While the for header executes i takes different values at different times during the same iteration.
Look at the condition and increment parts of the loop:
1 2
for ( int i = sizeof(size) ; i-- ; )
data.append((size >> (i * 8)) & 0xFF);
Converted to while loop:
1 2 3 4
int X = sizeof(size);
while ( X-- ) {
data.append((size >> (X * 8)) & 0xFF);
}
In the condition the tested values are: 4 3 2 1 0 (The 0 ends the loop.)
However, inside the body of the loop the X has values: 3 2 1 0
The OP version:
1 2 3
for ( int i = sizeof(size); i > 0 ; --i ) {
data.append((size >> (i * 8)) & 0xFF);
}
as while loop:
1 2 3 4 5
int X = sizeof(size);
while ( X > 0 ) {
data.append((size >> (X * 8)) & 0xFF);
--X;
}
In the condition the tested values are: 4 3 2 1 0 (The 0 ends the loop.)
And, inside the body of the loop the X has values: 4 3 2 1 (which does indeed look wrong)