I just need to extract those bytes using bitwise & operator. 0xFF is a hexadecimal mask to extract one byte. For 2 bytes, this code is working correctly:
#include <stdio.h>
int main()
{
unsigned int i = 0x7ee;
unsigned char c[2];
c[0] = i & 0xFF;
c[1] = (i>>8) & 0xFF;
printf("c[0] = %x \n", c[0]);
printf("c[1] = %x \n", c[1]);
return 0;
}
output:
c[0] = ee;
c[1] = 7;
What should I do for 4 bytes to work correctly?
unsigned int i = 0x557e89f3;
unsigned char c[4];
my code:
unsigned char c[4];
c[0] = i & 0xFF;
c[1] = (i>>8) & 0xFF;
c[2] = (i>>16) & 0xFF;
c[3] = (i>>24) & 0xFF;
printf("c[0] = %x \n", c[0]);
printf("c[1] = %x \n", c[1]);
printf("c[2] = %x \n", c[2]);
printf("c[3] = %x \n", c[3]);