Convert NSInteger to NSUInteger?
Asked Answered
H

4

25

I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this?

Herbie answered 7/8, 2011 at 5:38 Comment(0)
Z
36

NSInteger and NSUInteger are just typedefs for primitive integer types:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

As such, you don't need to "convert" between them. A simple cast should be sufficient. Like:

NSInteger myInt = 0;
NSUInteger unsignedInt = (NSUInteger)myInt;
Zobias answered 7/8, 2011 at 5:43 Comment(1)
Isn't casting a signed int to an unsigned int dangerous? I read that it may cause corruption as a sign bit may be interpreted as a value in unsigned ints? Or does C all the work to prevent that from happening?Sticktight
E
8

Since this might be useful for other coming across this issue here is a little table showing you the actual effect of casting. These values were taken straight from the debugger as hex values. Choose accordingly, as you can see casting does cause effects. For 32-bit, lop off the bottom ffffffff and for 16-bit lop off bottom ffffffffffff. Also note, -1 is always 0xffffffffffffffff.

NSInteger si = NSIntegerMax;    // si   NSInteger   0x7fffffffffffffff
si = -1;                        // si   NSInteger   0xffffffffffffffff
si = (NSInteger)-1;             // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)-1;            // si   NSInteger   0xffffffffffffffff
si = NSUIntegerMax;             // si   NSInteger   0xffffffffffffffff
si = (NSInteger)NSIntegerMax;   // si   NSInteger   0x7fffffffffffffff
si = (NSUInteger)NSIntegerMax;  // si   NSInteger   0x7fffffffffffffff
si = (NSInteger)NSUIntegerMax;  // si   NSInteger   0xffffffffffffffff
si = (NSUInteger)NSUIntegerMax; // si   NSInteger   0xffffffffffffffff

NSUInteger ui = NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = -1;                        // ui   NSUInteger  0xffffffffffffffff
ui = (NSInteger)-1;             // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)-1;            // ui   NSUInteger  0xffffffffffffffff
ui = NSIntegerMax;              // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSIntegerMax;   // ui   NSUInteger  0x7fffffffffffffff
ui = (NSUInteger)NSIntegerMax;  // ui   NSUInteger  0x7fffffffffffffff
ui = (NSInteger)NSUIntegerMax;  // ui   NSUInteger  0xffffffffffffffff
ui = (NSUInteger)NSUIntegerMax; // ui   NSUInteger  0xffffffffffffffff
Epimorphosis answered 23/2, 2012 at 23:14 Comment(0)
D
4

If you are certain that your NSInteger is greater than or equal to zero, you simply cast it as NSUInteger. You should not be casting to NSUInteger if your NSInteger represents a negative number, as it may lead to inconsistency.

NSInteger signedInteger = -30;
NSUInteger unsignedInteger = (NSUInteger)signedInteger;

results in

integer results

if (unsignedInteger == signedInteger) {
    // is TRUE
}
if (signedInteger == 4294967266) {
    // is FALSE
}
if (signedInteger == -30) {
    // is TRUE
}
if (unsignedInteger == 4294967266) {
    // is TRUE
}
if (unsignedInteger == -30) {
    // is TRUE
}
Dharana answered 25/3, 2014 at 18:5 Comment(1)
in NSObjCRuntime.h #define NSUIntegerMax ULONG_MAX NSLog(@"ULONG_MAX: %lu", ULONG_MAX); ULONG_MAX: 4294967295 4294967295-30+1 = 4294967266 +1 because of NSInteger ui = NSIntegerMax; ui = -1; ui = (NSUInteger)-1;Crannog
A
0

If you are wanting to convert an integer to an unsigned integer, you are either in a situation where you know that the integer will never be negative, or you may be wanting all negative values to be converted to absolute values (i.e., unsigned values). Doing this conversion to an absolute value is straightforward:

NSInteger mySignedInteger = -100;
NSUInteger myUnsignedInteger = fabs(mySignedInteger);

fabs() is a C function that returns an absolute value for any float. Here, myUnsignedInteger would have a value of 100.

Possible use cases for such a function would be display where you will indicate the negative values in a different format. For instance, in accounting negative numbers are displayed in parentheses:

NSString * numberForDisplay;
if (mySignedInteger < 0) {
    numberForDisplay = [NSString stringWithFormat:@"(%lu)", myUnsignedInteger];
} else {
    numberForDisplay = [NSString stringWithFormat:@"%lu", myUnsignedInteger];
}

Note that from a technical point of view, you could simply assign an NSInteger to an NSUInteger—this does not even require a cast—but when the NSInteger is negative the NSUInteger will return a very large positive number, which is clearly a highly undesirable side effect:

NSUInteger myUnsignedInteger = mySignedInteger;
Arelus answered 31/12, 2014 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.