2

Hi I am trying to make a Decimal to binary number converter in Objective-C but have been unsucessful... I have the following method so far which is an attempted translation from Java for a similar method. Any help to make this method work is much appreciated.

 +(NSString *) DecToBinary: (int) decInt
{
    int result = 0;
    int multiplier;
    int base = 2;
    while(decInt > 0)
    {
        int r = decInt % 2;
        decInt = decInt / base;
        result = result + r * multiplier;
        multiplier = multiplier * 10;
    }
return [NSString stringWithFormat:@"%d",result];
4
  • 1
    Related: stackoverflow.com/questions/7911651/decimal-to-binary (you'll need to convert the resulting strings to NSString, but objective c is a superset of c... Commented Mar 12, 2014 at 23:42
  • That is a rather bizarre algorithm. If you want to convert to character binary, why not go straight to character? (At the very least make result be a long, since it's apt to be a very large number.) Commented Mar 12, 2014 at 23:53
  • (And there are much more straight-forward ways to do this in Java, too.) Commented Mar 12, 2014 at 23:54
  • Incidentally, this isn't a "decimal to binary" converter, it's a numeric value to character binary converter. The incoming value is not represented as decimal but actually, internally, is (in all modern machines) a binary number. Commented Mar 12, 2014 at 23:56

2 Answers 2

11

I would use bit shifting to reach each bit of the integer

x = x >> 1;

moves the bits by one to the left, the decimal 13 is represente in bits as 1101, so shifting it to the right turns creates 110 -> 6.

x&1

is the bit masking x with 1

  1101
& 0001
------
= 0001

Combined these lines will iterate form the lowest to highest bit and we can add this bit as formatted integer to a string.

For unsigned int it could be this.

#import <Foundation/Foundation.h>

@interface BinaryFormatter : NSObject
+(NSString *) decToBinary: (NSUInteger) decInt;
@end

@implementation BinaryFormatter

+(NSString *)decToBinary:(NSUInteger)decInt
{
    NSString *string = @"" ;
    NSUInteger x = decInt;

    while (x>0) {
        string = [[NSString stringWithFormat: @"%lu", x&1] stringByAppendingString:string];
        x = x >> 1;
    }
    return string;
}
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *binaryRepresentation = [BinaryFormatter decToBinary:13];
        NSLog(@"%@", binaryRepresentation);
    }
    return 0;
}

this code will return 1101, the binary representation of 13.


Shorter form with do-while, x >>= 1 is the short form of x = x >> 1:

+(NSString *)decToBinary:(NSUInteger)decInt
{
    NSString *string = @"" ;
    NSUInteger x = decInt ;
    do {
        string = [[NSString stringWithFormat: @"%lu", x&1] stringByAppendingString:string];
    } while (x >>= 1);
    return string;
}
Sign up to request clarification or add additional context in comments.

1 Comment

What is the reason for the variable i in the longer version? It seems that it is not used in the loop for nothing than incrementing?
0
NSMutableArray *arr = [[NSMutableArray alloc]init];

//i = input, here i =4
i=4;

//r = remainder
//q = quotient

//arr contains the binary of 4 in reverse order
while (i!=0)
{
    r = i%2;
    q = i/2;
    [arr addObject:[NSNumber numberWithInt:r]];
    i=q;
}
NSLog(@"%@",arr);

// arr count is obtained to made another array having same size
c = arr.count;

//dup contains the binary of 4
NSMutableArray *dup =[[NSMutableArray alloc]initWithCapacity:c];    

for (c=c-1; c>=0; c--)
{
    [dup addObject:[arr objectAtIndex:c]];
}

NSLog(@"%@",dup);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.