The Spk
The Spk

Reputation: 1

Converting a string variable from Binary to Decimal in Objective C

Im trying to create a Binary to Decimal calculator and I am having trouble doing any sort of conversion that will actually work. First off Id like to introduce myself as a complete novice to objective c and to programming in general. As a result many concepts will appear difficult to me, so I am mostly looking for the easiest way to understand and not the most efficient way of doing this. I have at the moment a calculator that will accept input and display this in a label. This part is working fine and I have no issues with it. The variable that the input is stored on is _display = [[NSMutableString stringWithCapacity:20] retain]; this is working perfectly and I am able to modify the data accordingly. What I would like to do is to be able to display an NSString of the conversion in another label. At the moment I have tried a few solutions and have not had any decent results, this is the latest attempt

    - (NSMutableString *)displayValue2:(long long)element
{
    _str= [[NSMutableString alloc] initWithString:@""];     
    if(element > 0){
        for(NSInteger numberCopy = element; numberCopy > 0; numberCopy >>= 1)
        {

            [_str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
        }
}
    else if(element == 0)
    {
        [_str insertString:@"0" atIndex:0];
    }
    else
    {
        element = element * (-1);

        _str = [self displayValue2:element];
        [_str insertString:@"0" atIndex:0];
        NSLog(@"Prima for: %@",_str);
        for(int i=0; i<[_str length];i++)

        _str = _display;
        NSLog(@"Dopo for: %@",_str);
    }
    return _str;

}

Within my View Controller I have a convert button setup, when this is pressed I want to set the second display field to the decimal equivalent. This is working as if I set displayValue2 to return a string of my choosing it works. All I need is help getting this conversion to work. At the moment this bit of code has led to "incomplete implementation" being displayed at the to of my class. Please help, and cheers to those who take time out to help.

Upvotes: 0

Views: 1462

Answers (1)

Firo
Firo

Reputation: 15566

So basically all you are really looking for is a way to convert binary numbers into decimal numbers, correct? Another way to think of this problem is changing a number's base from base 2 to base 10. I have used functions like this before in my projects:

+ (NSNumber *)convertBinaryStringToDecimalNumber:(NSString *)binaryString {
    NSUInteger totalValue = 0;
    for (int i = 0; i < binaryString.length; i++) {
        totalValue += (int)([binaryString characterAtIndex:(binaryString.length - 1 - i)] - 48) * pow(2, i);
    }
    return @(totalValue);
}

Obviously this is accessing the binary as a string representation. This works well since you can easily access each value over a number which is more difficult. You could also easily change the return type from an NSNumber to some string literal. This also works for your element == 0 scenario.

// original number wrapped as a string
NSString *stringValue = [NSString stringWithFormat:@"%d", 11001];
// convert the value and get an NSNumber back
NSNumber *result = [self.class convertBinaryStringToDecinalNumber:stringValue];
// prints 25
NSLog(@"%@", result);

If I misunderstood something please clarify, if you do not understand the code let me know. Also, this may not be the most efficient but it is simple and clean.

I also strongly agree with Hot Licks comment. If you are truly interested in learning well and want to be an developed programmer there are a few basics you should be learning first (I learned with Java and am glad that I did).

Upvotes: 1

Related Questions