Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C - Unsigned Int to Unsigned Char Conversion

Status
Not open for further replies.

Analog Kid

Newbie level 6
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top