unsigned char buff[2];
unsigned short value;
value = (buff[0] << 8) + buff[1];
2 byte char -> unsigned short
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?
Use a union; it''s the easy way out:
You can then access either val, buf[0] or buf[1].
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
union doublechar { char buf[2]; short val;};
You can then access either val, buf[0] or buf[1].
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
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];
unsigned char buff[2];
unsigned short value;
value = (((unsigned short)buff[0]) << 8) + buff[1];
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement