开发者

iPhone int parsing

开发者 https://www.devze.com 2023-03-26 16:22 出处:网络
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'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 开发者_StackOverflow1304287200000.

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?


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.


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.


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


Your number exceeding integer limits

0

精彩评论

暂无评论...
验证码 换一张
取 消