0

I'm making a binary converter for iPhone, and for convert a number from base X to other base I use this method:

+(NSString*)FromXToOtherBase:(unsigned long long)base number:(unsigned long long)number
{

  unsigned long long temp_risu=number;
   //int i=0;
   //base is the destination base
   NSMutableArray*rest=[[NSMutableArray alloc] init];
   while(temp_risu!=0)
   {
      NSNumber*rest_=[NSNumber numberWithLongLong:(temp_risu%base)];
      [rest addObject:rest_];
      temp_risu=temp_risu/base;
      rest_=Nil;

    }

   NSMutableArray*rest_reverse=[[NSMutableArray alloc] init];

   for(int i=[rest count]-1;(i>-1);i--)
   {
     [rest_reverse addObject:[rest objectAtIndex:i]];
   }

   return [rest_reverse componentsJoinedByString:@""];
}

This method works but when the number becomes bigger and bigger I have problem with the range of unsigned long long type and the conversion becomes unreliable. How Do I solve this problem?

Alessio
  • 13
  • 5
  • This is weird... if you say the conversion becomes unreliable, then your code is not robust. Your code MUST be reliable, and if there's a condition that it can't handle, it should generate an error. – Merlevede Feb 20 '14 at 21:58
  • "unreliable" means that when I convert a big number(for example 1111111111) the conversion is incorrect,but if I remove a number(111111111)the conversion is right. For this reason I think that it's a representation problem – Alessio Feb 20 '14 at 22:14
  • To which base are you converting your big number? – Merlevede Feb 20 '14 at 22:23
  • The range of `unsigned long long` is something much bigger like 0 to 9223372036854775807 * 2 in GCC. Are you expecting to send any a parameter bigger than that also? – Ayan Sengupta Feb 20 '14 at 22:33
  • @Merlevede from 10 to 2 – Alessio Feb 20 '14 at 22:36
  • I don't think it,but I don't understand because when the number becomes big the conversion is wrong – Alessio Feb 20 '14 at 22:38
  • https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsnumber_class/Reference/Reference.html suggest you to read the NOTE – Coldsteel48 Feb 20 '14 at 22:46
  • Also this is the methodu need https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsnumber_class/Reference/Reference.html#//apple_ref/occ/instm/NSNumber/initWithUnsignedLongLong: – Coldsteel48 Feb 20 '14 at 22:47
  • you assigned it to Signed number, so yes when it become bigger , the conversion will be wrong since , you should init it with above parameter as I linked – Coldsteel48 Feb 20 '14 at 22:49
  • @user3323294 Man your code works!!! I don't understand the problem – Merlevede Feb 20 '14 at 22:50
  • @Merlevede use bigger numbers to convert , when the first bit of Signed number will be 1 , u will not have expected value(I guess). – Coldsteel48 Feb 20 '14 at 22:51
  • I copied & pasted the code, I tested with huge numbers, and it works. I think we're wasting our time here. – Merlevede Feb 20 '14 at 22:55
  • When I call method: [ConversionClass FromXToOtherBase:2 number:1111110111110101111111111]) There is a warning message:"Integer constant is too large for its type" – Alessio Feb 20 '14 at 22:57
  • @ROma-MT If I understand I must use "initWithLongLong" method? – Alessio Feb 20 '14 at 22:59
  • define this number as `unsigned long long ` since when you type just a number it interprets like `int` – Coldsteel48 Feb 20 '14 at 22:59
  • @user3323294 `initWithUnsignedLongLong` Method , in your original code u did `initWithLongLong` which is SIGNED. – Coldsteel48 Feb 20 '14 at 23:01
  • https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSNumber/numberWithUnsignedLongLong: Here is a documentation. – Coldsteel48 Feb 20 '14 at 23:02
  • I have done so: unsigned long long x=1111110111110101111111111; NSLog(@"%@",[ConversionClass FromXToOtherBase:2 number:x]); But I get the same warning message – Alessio Feb 20 '14 at 23:08
  • Where do you get the warning , on what line ?> – Coldsteel48 Feb 20 '14 at 23:12
  • on "unsigned long long x=1111110111110101111111111;" line – Alessio Feb 20 '14 at 23:15
  • For example if I insert this number in 10 base:"1111110111110177333245". I should get "1111000011101111000000000101110010010000011010111011100000000000000000" but I get instead:"11101111000000000101110010010000011010111011011001111111111101" – Alessio Feb 20 '14 at 23:19
  • Ok what u expect has 70 bits length , and what u get has 62 . which starts to make sense. I think long long is just to little type to represent 70 bits. – Coldsteel48 Feb 20 '14 at 23:37
  • Here you can learn about premetive types in iOS http://stackoverflow.com/questions/2107544/types-in-objective-c-on-iphone – Coldsteel48 Feb 20 '14 at 23:38
  • Ok I'm beginning to understand , but I have a question:"Why this website(http://wims.unice.fr/wims/wims.cgi?session=7KD56BA8C7.1&lang=it&cmd=reply&module=tool%2Fnumber%2Fbaseconv.it&input=1111110111110177333&ibase=10&obase=2&prec=30) can convert any number without problem? Which data type uses? – Alessio Feb 20 '14 at 23:56

1 Answers1

0

Well for me it seems like , you assignUnsigned long long in to signed long long

   NSNumber*rest_=[NSNumber numberWithLongLong:(temp_risu%base)];

change this line with

    NSNumber*rest_=[NSNumber numberWithUnsignedLongLong:(temp_risu%base)];

I think it should be enough.

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43