C program to extract bytes from an integer (Hexadecimal) value.

This program will extract bytes values from an integer (hexadecimal) value. Here we will take an integer value in hexadecimal format and then extract all 4 bytes in different four variables. The logic behind to implement this program - right shift value according to byte position and mask it for One byte value (0xff).

Extract bytes from an integer (Hexadecimal) value using C program

/*C program to extract bytes from an integer (Hex) value.*/

#include <stdio.h>

typedef unsigned char BYTE;

int main()
{
    unsigned int value=0x11223344; //4 Bytes value
    
    BYTE a,b,c,d; //to store byte by byte value
    
    
    a=(value&0xFF); //extract first byte
    b=((value>>8)&0xFF); //extract second byte
    c=((value>>16)&0xFF); //extract third byte
    d=((value>>24)&0xFF); //extract fourth byte

    printf("a= %02X\n",a);
    printf("b= %02X\n",b);
    printf("c= %02X\n",c);
    printf("d= %02X\n",d);
 
    return 0;
}
    
    a= 44
    b= 33
    c= 22
    d= 11