C - Unsigned Int to Unsigned Char Conversion

Status
Not open for further replies.

Analog Kid

Newbie level 6
Joined
Dec 9, 2001
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Toronto, Ontario, Canada
Activity points
14
unsigned int to char conver c

Can't seem to get this to work in 'C' !
Here's what I want to do in simple terms:
X = 0xF840 unsigned int
split the two bytes up to get:
Y = 0xF8 unsigned char
Z = 0x40 unsigned char

using Hi-Tech C.
HELP!
 

int to unsigned char

There are two ways to do it using Hi-Tech for PIC:

y=(unsigned char) (x >> 8);
z=(unsigned char) x; or z=x & 255;
------------------------------------------
union
{
unsigned int word;
unsigned char bytes[2];
}x;

x.word=0xF840;
y=x.bytes[1];
z=x.bytes[0];
------------------------------------------

hope this helps and best regards
 

    Analog Kid

    Points: 2
    Helpful Answer Positive Rating
also do:

unsigned int word = 0xf840;
unsigned char *pByte = (unsigned char *) &word;
x= pByte[1];
y= pByte[0];
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…