nsstring - Strange Overflow - Objective-C -


i trying solve problem 22 project euler, following code :

        nsarray *alphabet = [nsarray arraywithobjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];     nserror *error = nil;       nsstring *file  = [[nsbundle mainbundle] pathforresource:@"names" oftype:@"txt"];     nsstring *names = [[nsstring alloc] initwithcontentsoffile: file                                                       encoding: nsasciistringencoding                                                          error: &error];       if (names == nil) {         [nsexception raise:@"error reading file :" format:@"%@",error];     }     nsmutablearray *namesarray = [nsmutablearray arraywitharray:[names componentsseparatedbystring:@","]];     unsigned long long int sum = 0;     unsigned long long int partsum = 0;     (nsstring *str in namesarray){         partsum = 0;         (int = 0; < [str length]; i++) {             partsum += [alphabet indexofobject:[nsstring stringwithformat:@"%c",[str characteratindex:i]]]+1;             nslog(@"%lli",partsum);         }         nslog(@"%@ - %lli",str,partsum);         sum += [namesarray indexofobject:str]*partsum;     }     nslog(@"%lli",sum); 

there no problems getting names array when log sum , partsum variables strange values -9223372036854775755. far know, due overflow, looking when program computes name on example, following output :

-9223372036854775808 -9223372036854775805 -9223372036854775790 -9223372036854775778 -9223372036854775769 -9223372036854775755 53 "colin" - 53 

the first values overflowed last result strangely correct (not case names). why such overflow? numbers should 3, 18, 30, 39, 53. guess program treats " characters , looks index in alphabet. that, lot on net, couldn't find how delete them string. can't use [str stringbyreplacingoccurrencesofstring:@""" withstring:@""]. suggestions?

your guess right. @ point @ " indexofobject returns nsnotfound, constant value maxint. @rmaddy pointed out, nslog trying print unigned value signed one, , end big negative number.

for removing " try

[str stringbyreplacingoccurrencesofstring:@"\"" withstring:@""]; 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -