int to unsined int

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
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
Topic archived. No new replies allowed.