Just use memcpy:
#include <string.h> float f = 2.45f; char a[sizeof(float)]; memcpy(a, &f, sizeof(float));
If you need the opposite endianness, then the trivial question is to change the bytes in a subsequently, for example.
int i, j; for (i = 0, j = sizeof(float) - 1; i < j; ++i, --j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; }
Paul r
source share