You convert your int into bytes, for example, like this:
void Uint2Uchars(unsigned char* buf, unsigned int n)
{
memcpy(buf, &n, sizeof n);
}
Or, if you're interested in a particular endianness, you could do this:
void Uint2UcharsLE(unsigned char* buf, unsigned int n)
{
size_t i;
for (i = 0; i < sizeof n; i++)
{
buf[i] = n;
n >>= CHAR_BIT;
}
}
or
void Uint2UcharsBE(unsigned char* buf, unsigned int n)
{
size_t i;
for (i = 0; i < sizeof n; i++)
{
buf[sizeof n - 1 - i] = n;
n >>= CHAR_BIT;
}
}
Don't forget to include the appropriate headers, <string.h>
and <limits.h>
as applicable.