int to unsined int

Jun 29, 2012 at 1:49am
How can I convert int to unsined int and inversely?

Is it correct to cast it?

1
2
int i = 0
unsigned int = (unsigned int) i
Last edited on Jun 29, 2012 at 2:06am
Jun 29, 2012 at 2:31am
Depends on how do you plan to handle negative values of the int when converting to unsigned and how do you plan to handle the values of the unsigned int that are larger than INT_MAX, when converting to signed.

If the rules of the standard integer conversion are acceptable, then it's okay to cast, implicitly or explicitly (the result is the same):

1
2
3
int i = 0;
unsigned int un = i; // or static_cast, or C-style cast
int i2 = un; // likewise 
Last edited on Jun 29, 2012 at 2:31am
Topic archived. No new replies allowed.