tags:

views:

707

answers:

4

Hi, all

I have long integer value ( ex: 2705758126 ) in NSString.

When i try to show it: NSLog(@"%i", [myValue integerValue]); it return: 2147483647.

How to show, compare and etc. this long integer

+5  A: 

Try with

NSLog(@"%lld", [myValue longlongValue]);
IlDan
+3  A: 

The documentation recommends using @"%ld" and @"%lu" for NSInteger and NSUInteger, respectively. Since you're using the integerValue method (as opposed to intValue or longLongValue or whatever), that will be returning an NSInteger.

Dave DeLong
he has a long integer
IlDan
@IlDan No, he has an NSString that he's converting to an NSInteger which means it might be an int (on 32-bit machines) or it might be a long it (on 64-bit machines).
Dave DeLong
Oh well, I tend to listen to what people say. I'm reading "I have long integer value".
IlDan
@IlDan yeah I missed the NSString bit the first time reading it, too. =)
Dave DeLong
Seems to me the poster has a string that contains a number which will overflow a 32 bit value. Therefore NSInteger won't be good enough on a 32 bit machine.
Stig Brautaset
@Stig 2^32 = 4,294,967,296, which is 1,589,209,170 larger than the number given in the question. How would it not fit in a 32-bit int?
Dave DeLong
@Dave DeLong the method `integerValue` returns a signed number. the signed 32 bit int's max is (2^31)-1, not 2^32
Justin
A: 

You should have a look at Apple's documentation. NSString has the following method:

- (long long)longLongValue

Which should do what you want.

Stig Brautaset
A: 

from this example here, you can see the the conversions both ways:

NSString *str=@"5678901234567890";

long long verylong;
NSRange range;
range.length = 15;
range.location = 0;

[[NSScanner scannerWithString:[str substringWithRange:range]] scanLongLong:&verylong];

NSLog(@"long long value %lld",verylong);

zoltan