Wednesday, 15 June 2011

How do I convert and break a 2 byte integer into 2 different chars in C? -



How do I convert and break a 2 byte integer into 2 different chars in C? -

i want convert unsigned int , break 2 chars. example: if integer 1, binary representation 0000 0001. want 0000 part in 1 char variable , 0001 part in binary variable. how accomplish in c?

if insist have sizeof(int)==2 then:

unsigned int x = (unsigned int)2; //or other value happens unsigned char high = (unsigned char)(x>>8); unsigned char low = x & 0xff;

if have 8 bits total (one byte) , breaking 2 4-bit values:

unsigned char x=2;// or whatever unsigned char high = (x>>4); unsigned char low = x & 0xf;

c

No comments:

Post a Comment