RE: union to get parts of integer
"Reza Hoorfar" <[email protected]> Mon, 20 Dec 2010 15:33:06 +0330
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
union MyUnion{
int myInt;
char myChar[sizeof(int)];
};
void main()
{
MyUnion m;
m.myInt = 0x01020304;
printf("value :%d\n",m.myChar[0]); // -> value : 4
printf("value :%d\n",m.myChar[1]); // -> value : 3
printf("value :%d\n",m.myChar[2]); // -> value : 2
printf("value :%d\n",m.myChar[3]); // -> value : 1
}
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of ratheesh k
Sent: Monday, December 20, 2010 2:10 PM
To: [email protected]
Subject: union to get parts of integer
typedef struct {
char parts[4];
} node ;
int i=0x12345678
((node *)&i)->parts[0];
((node *)&i)->parts[1];
((node *)&i)->parts[2];
((node *)&i)->parts[3];
Is there any mechanism to split into bytes using the power of union ?
-Ratheesh
--
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html