0

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.

1
  • 1
    A) If you're a complete novice to programming in general, what the heck are you doing programming in Objective-C. You should at the very least learn Java first, if not C or C++. B) learn how to write comments. C) "Incomplete implementation" means that you left out a method of the class that you declared in the .h file. D) NSString has a number of conversion methods -- doubleValue, intValue, boolValue, etc. E) "Binary to decimal calculator" is a poor description of anything. Do you enter character binary and display in character decimal? Commented Oct 24, 2013 at 16:19

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the help but I do find that a bit difficult to understand and implement. With regards my experience, I have done Java, we skipped through it fast, probably way too fast and im far from fully efficient. Ive also done some Actionscript. Now we are doing objective C, and again moving fast, only introduced to it 5 weeks ago. This is a college course where Im made to do programming assignments, im not doing it for fun. I declared myself a novice as I dont want to give people the impression im well versed in this. mostly I have just found this board condescending to those asking for help.
What exactly should I put in my .h file to get this to work. Also I have a button in my view controller which I want to run this bit of code " _displayField.text= [calculator displayValue2];" when the button is pushed it should set my display to the string returned from displayValue2.
@TheSpk I understand your concerns from your first comment. You do not need to put anything in your .h (most likely). Just place this method in your implementation. Then in your action button place my sample code (in my second block). After you receive the result place it into the new field.
think im getting an idea, but instead of the "1001" string you have is there a way I can read the data from [calculator displayValue]
Yes, but I do not know what calculator is or what displayValue returns (are you sure it does not already return a string?).
|

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.