Sign in to follow this  
thedustbustr

how to c++ cast from int so ushort

Recommended Posts

the int will always be positive and within the range of possible values for a short. external library has poor design. int x = getWidth(); unsigned short xx = x; //how to cast this in c++

Share this post


Link to post
Share on other sites
unsigned short xx = static_cast<unsigned short>(x);


Quote:
Original post by ToohrVyk
EDIT: mine's formatted better [wink]


Not any more [razz].

Share this post


Link to post
Share on other sites
The reinterpret_cast is bad for a lot of reasons. Number one of which is that it won't work properly in a number of situations. For example, using it on a big-endian machine will cause it to reinterpret the wrong set of bytes. There's no reason to get cute when doing a simple cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
The reinterpret_cast is bad for a lot of reasons. Number one of which is that it won't work properly in a number of situations. For example, using it on a big-endian machine will cause it to reinterpret the wrong set of bytes. There's no reason to get cute when doing a simple cast.


I completely agree. I was indeed trying to be cute, and in no way meant it to be taken seriously.

Share this post


Link to post
Share on other sites
Why are you copying it to an unsigned short? What do you gain? The fact that it is "always" positive and that it fits in a short are not really very good reasons.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this