Andreas Eriksson
Andreas Eriksson

Reputation: 9027

iPhone int parsing

I'm working on an iPhone app. I want to parse a series of numbers from a string. However, intValue is acting really really strange.

I have a string with the value 1304287200000.

I then place the intValue of that into an NSInteger, and lo and behold, my integer is for some reason assigned the value of 2147483647.

What gives?

Upvotes: 2

Views: 109

Answers (4)

user94896
user94896

Reputation:

int is 32-bit, so the maximum value it can hold is 2,147,483,647. Try longLongValue instead.

Upvotes: 2

zoul
zoul

Reputation: 104065

What you are getting back is INT32_MAX, because the parsed value overflows the int type. This is explained in the NSString documentation. Try longLongValue instead, LLONG_MAX should be big enough.

Upvotes: 4

PeyloW
PeyloW

Reputation: 36752

The datatype int is a 32bit numeric value, with a range of approximately ±2 billion. 1304287200000 is by a margin outside of that range.

You need to skip int for long long that is a 64bit type and covers your need. A more human readable and explicit name for the 64bit type is int64_t.

Upvotes: 4

nidhin
nidhin

Reputation: 6920

Your number exceeding integer limits

Upvotes: 1

Related Questions