You can do it using bit fiddling:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned char source[3] = { 15, 85, 51 };
unsigned char destination[4];
memset(destination, 0, 4);
for (int i = 0; i < (8 * 3); ++i)
{
destination[i / 6] |= ((source[i / 8] >> (i % 8) & 1) << (i % 6));
}
for (int j = 0; j < 4; ++j)
printf("%d ", destination[j]);
}
Output:
15 20 53 12
Note that this starts working from the five least significant bits.
15 85 51
11110000 10101010 11001100
111100 001010 101011 001100
15 20 53 12
To get most significant first, do this instead:
destination[i / 6] |= ((source[i / 8] >> (7 - (i % 8))) & 1) << (5 - (i % 6));
This works as in your example, assuming you wrote the most significant bit first:
240 170 204
11110000 10101010 11001100
111100 001010 101011 001100
60 10 43 12