2

Hi I have made an IOS app that converts binary, hexadecimal and decimal values. It all works fine except for my decimal to binary conversion. Here is what I have. It returns 0s and 1s but far too many. Can anyone tell me why this is or help me with a better method?

NSString *newDec = [display text]; //takes user input from display
NSString *string = @"";
NSUInteger x = newDec;
int i = 0;
while (x > 0) {
    string = [[NSString stringWithFormat:@"%u", x&1] stringByAppendingString:string];
    x = x>> 1;
    ++i;
}
display.text = string; //Displays result in ios text box 
1
  • I tested you code, and it works fine Commented Mar 15, 2014 at 16:33

1 Answer 1

3

Try this:

NSUInteger x = [newDec integerValue];

And next time don't ignore the Compiler's "Incompatible pointer to Integer conversion" hint...

Explanation: Afaik, assigning an object to an int, actually assigns the address of the object to that integer, not the content of the string (which is what you want).

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

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.