Archived

This topic is now archived and is closed to further replies.

_Josh

2 byte char -> unsigned short

Recommended Posts

_Josh    122
I''m trying to take the contents of a character array of two bytes and convert it to an unsigned short. The problem is that my formula always comes up 256 short. Can anyone explain why?
unsigned char buff[2];
unsigned short value;

value = (buff[0] << 8) + buff[1];
 

Share this post


Link to post
Share on other sites
a person    118
because you are shifting your upper bits out the register.

unsigned char buff[2];
unsigned short value;
value = (((unsigned short)buff[0]) << 8) + buff[1];

Share this post


Link to post
Share on other sites