Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
int variable = 0x12345678; /* 4 byte integer containing some value */
char byte0, byte1, byte2, byte3; /* Variables to hold the char-values */
/* extract the bytes as characters */
byte0 = (char)((variable >> (0*8)) & 0x000000FF);
byte1 = (char)((variable >> (1*8)) & 0x000000FF);
byte2 = (char)((variable >> (2*8)) & 0x000000FF);
byte3 = (char)((variable >> (3*8)) & 0x000000FF);
/* Another way using pointers */
byte0 = ((char*)(&variable))[0];
byte1 = ((char*)(&variable))[1];
byte2 = ((char*)(&variable))[2];
byte3 = ((char*)(&variable))[3];
Thank you very much for your quick response I was reading this code. whats happening in program. why there is same value &0xFF in all statementWell, if you have 4-byte integer, you can extract the individual bytes and cast them to char-variable. There are many ways to do it.
C:int variable = 0x12345678; /* 4 byte integer containing some value */ char byte0, byte1, byte2, byte3; /* Variables to hold the char-values */ /* extract the bytes as characters */ byte0 = (char)((variable >> (0*8)) & 0x000000FF); byte1 = (char)((variable >> (1*8)) & 0x000000FF); byte2 = (char)((variable >> (2*8)) & 0x000000FF); byte3 = (char)((variable >> (3*8)) & 0x000000FF); /* Another way using pointers */ byte0 = ((char*)(&variable))[0]; byte1 = ((char*)(&variable))[1]; byte2 = ((char*)(&variable))[2]; byte3 = ((char*)(&variable))[3];
#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;
}