G'day,
I'm currently writing some endian-indepedant code. Specifically from files in little-endian. I have a few functions in place to swap the bytes of a character array:
What's the most portable way to unpack/pack the values of the array into say, integer variables? Float variables and alike? I could use bit shifting etc, but is there a better way?Code:static void endian_swap_bytes(char * array, size_t n) { size_t i = 0; size_t half = 0; char tmp; if(array == NULL || n <= 1) return; half = n / 2; while(i < half) { --n; tmp = array[i]; array[i] = array[n]; array[n] = tmp; ++i; } return; }
Thanks in advance