-2
- (int)makeBinary:(int)toChange{

    int inNumber;
    int outNumber;
    NSString *outputString;
    outputString=@"";
    int temp;

    inNumber=toChange;

    for (int b=0; b<=7; b++) {
        temp=inNumber%2;
        inNumber=(inNumber-temp)/2;
        outputString=[outputString stringByAppendingFormat:@"%i", temp];
    }

        outNumber=[outputString intValue];

    return outNumber;
}

That works fine, but how can i reverse the output?

2
  • possible duplicate of How to Convert NSInteger to a binary (string) value Commented Jun 18, 2011 at 15:53
  • As posed, your question makes no sense. You have a method that takes an int and returns an int. "how can I reverse the output?" doesn't mean anything. Commented Jun 18, 2011 at 17:12

2 Answers 2

1

First, an int is neither binary nor decimal, it is an integer, binary and decimal are refers to the representation of the number. You can do (in your loop):

outputString=[NSString stringWithFormat:@"%i%s", temp, outputString];

But again, the number is the same number, the representation might change.

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

4 Comments

Where did i write that an int is a binary or a decimal value?
You try to convert a number from decimal to binary, when both input and output are ints.
sorry, i don't get it. What is the problem with that?. (I realized that its better when i return a string and not an int. But thats completely unnecessary for the moment)
There's no problem with that. The number is stored in binary (as you already know) but if you want to show it in a binary format, you'll have to create such a function (as you described in your question)
0

This function is unnecessary. The computer already stores all ints in a binary representation. The only difference is how you interact with them. So, for example, bitwise operations (|, &, ~, ^, <<, >>) work on the number bit-by-bit, while other operators (+, -, *, /) don't work any differently if you're thinking of the ints as decimal or binary numbers (heck, you could imagine they were in any base you like and they'll still accomplish the same thing). The decimal representation of ints is just a nice way the computer has of showing you the value. Underneath it's all binary.

1 Comment

I know that everything is stored in binary (!?) But if i have an int value like 255, how can i store the binary representation of this number in 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.