What is the best method (performance) to put an
data = length & 0xff;
data = (length >> 8) & 0xff;
data = (length >> 16) & 0xff;
data = (length >> 24) & 0xff;
Are you looking for memcpy
char x; int a; memcpy(&a,x,sizeof(int));
Your solution is also good as it is endian safe.
On a side note:-
Although there is no such guarantee that
sizeof(int)==4 for any particular implementation.