iPhone int parsing - iphone

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?

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

Related

atoi() is not converting properly

I was trying to call atoi on the strings 509951644 and 4099516441. The first one got converted without any problem. The second one is giving me the decimal value 2,147,483,647 (0x7FFFFFFF). Why is this happening?
Your second integer is creating an overflow. The maximum 32-bit signed integer is 2147483647.
It's generally not recommended to use atoi anyway; use strtol instead, which actually tells you if your value is out of range. (The behavior of atoi is undefined when the input is out of range. Yours seems to be simply spitting out the maximum int value)
You could also check if your compiler has something like a atoi64 function, which would let you work with 64-bit values.
2147483647 is the maximum integer value in C (signed). It is giving the max that it can... the original is too large to convert to signed int. I suggest looking up how to convert into an unsigned int.

srandom(time(NULL)) giving warning - pointer to integer without a cast

In iPhone (Xcode 4), using the function,
srandom(time(NULL));
both srand and srandom is giving this warning. But when running its working fine.
Why I am getting the warning in one of my class file? I have used that in other files, but no warning.
Warning: passing argument 1 of 'srand' makes integer from pointer
without a cast
However, using arc4random() can solve this problem. But in most example srand() is used in this way and nobody complains. Thats why I am confused.
Because srand is expecting an integer and time() is returning a pointer (from the looks of your particular error). Casting explicitly to an int will make it go away. Or perhaps reading the pointer to get the actual time value might be what you are looking for instead. Not 100% certain of time's return value here, but I'd double check to make sure it is indeed returning a tics value instead of a pointer to a time_t object that will remain mostly the same over time.
According to what I just read, it's supposed to return a time_t value, which when cast as an int, is the number of seconds elapsed since 1972ish. So not a pointer usually, but in your case it may be. Either way, add either a dereference and a cast, or just a cast if you can get it to return the time_t directly.

Why is pow() reporting an infinite value in this case?

I am using the pow() function like this in my application:
float IValuePlusOne = (pow(2.25,120));
but it is reporting back an infinite value, instead of the correct result. I even tried the long long and long double data types but couldn't get them to produce the proper output.
Is there any other data type I need to use or do I need to make some other changes in my code?
As others have pointed out, you're losing precision and reducing the size of value you can represent by casting to a float. Running the following code:
double IValuePlusOne = pow(2.25,120.0);
NSLog(#"Test value: %f", IValuePlusOne);
on my iPhone gives the output:
Test value: 1827688475348373523156051712429585892114432.000000
which looks to be correct (1.827x10^42).
If you want to do calculations on values that a double can't hold, use NSDecimalNumber.

How to pass values to mex files

Hi I want to send a value from my matlab to my mex function. The value is generally about 10 digits long and i have used unsigned long long data type.
But i have difficulty accessing it from the mexfile.
mxGetPr returns double type, so is there some type conversion i have to do?
Yes, I just encountered this. You shouldn't use mxGetPr anymore in general. The better way to do it is to first check the type like this:
if(!mxIsClass(prhs[0],"double"))
{
mexErrMsgTxt("Data must be of type double!!!\n");
}
Then access the data through (double *)mxGetData(prhs[0]) or in your case (unsigned long long int*)mxGetData(prhs[0])
You can look up mxIsClass and mxGetData for more info.
Edit: Also here's a list of the different types for mxIsClass

NSString to 64-bit integer conversion on iPhone

I am struggling with very simple thing:
I receive some ids by http request as a string. I know they represent 64-bit integer id numbers.
How can I convert them to the 64-bit Integers (NSNumber or NSInteger)?
Functions like:
[nsstring integerValue],
[nsstring intValue]
seems to be 32bit limited (max value:2147483647).
Any Hints?
Data model with 64bit integer properties compiles fine so it means iPhone supports such a numbers for a god sake.
It must be some simple conversion method. It is so popular in http connection based devices.
have you tried longLongValue ?
if they have a fixed length, you could cut them on each 9th number, make the intvalue of the cutted parts and add the values with the 10^9-factor. surely not the most elegant way, but it should work