• Advertisement

Archived

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

2 byte char -> unsigned short

This topic is 5837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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

  • Advertisement