How convert unsigned int to unsigned char array

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;


c[0] = ee;
c[1] = 7;

What should I do for 4 bytes to work correctly?

unsigned int i = 0x557e89f3;
unsigned char c[4];

Source: c#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.