idoodler
idoodler

Reputation: 3545

Convert String to UInt16 Objective-C

I am facing the problem, that I can't convert an string or an int to an UInt16. This is my current code: (UInt16)[[defaults stringForKey:@"port"] intValue] [[defaults stringForKey:@"port"] intValue] is 8080 and (UInt16)[[defaults stringForKey:@"port"] intValue] becomes 51075 which is wrong. Does anyone know to do this?

Thanks for your help!

Upvotes: 1

Views: 1520

Answers (1)

idoodler
idoodler

Reputation: 3545

I found the answer my selfe:

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
    NSString *port = [defaults stringForKey:@"port"];
    UInt16 portNumber = [[formatter numberFromString:port] unsignedShortValue];

Upvotes: 2

Related Questions