Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shift left? shift right?

Status
Not open for further replies.

rayferd2

Technical User
Jun 24, 2003
2
US
hi everyone, this is my first post.

I'm not a total noob to C but my skills are rusty....it's been a while.

Here is what I have:

BYTE cin[133];
WORD msg[66];
.
.
where cin holds the values read from the serial port. the box I'm writing code for only reads one char at a time from the port. I want to take this data and map it over to a WORD as follows:

lets say cin[0]=FF and cin[1] =AC.....I want msg[0]=FFAC.

help?
 
lets say cin[0]=FF and cin[1] =AC.....I want msg[0]=FFAC.

Try
Code:
msg[0] = (((WORD)cin[0])<< 8 ) | cin[1];
 
There aren't any BYTE or WORD types in C. But assuming that BYTE is unsigned char and WORD is unsigned int:

msg[0] = cin[0] << 8 | cin[1];
 
Just do remember that the consequences of what you are doing are different in apples to PCs. If you are doing this on a PC, you are swapping the byte order, since a PC expects the low byte first, high byte second, whereas you're interpreting your data mac-way-round.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top