14

I am trying to figure out how to convert an NSInteger, say 56, to an NSString that is a binary representation of the original (int) value. Perhaps someone knows a formatting technique that can accept 56 and return "111000" within Objective C. Thanks All.

0

3 Answers 3

27

There's no built-in formatting operator to do that. If you wanted to convert it to a hexadecimal string, you could do:

NSString *str = [NSString stringWithFormat:@"%x", theNumber];

To convert it to a binary string, you'll have to build it yourself:

NSMutableString *str = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = theNumber; numberCopy > 0; numberCopy >>= 1)
{
    // Prepend "0" or "1", depending on the bit
    [str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
Sign up to request clarification or add additional context in comments.

1 Comment

You get one of those yellow errors if you don't do NSMutableString *str = [NSMutableString stringWithFormat:@""];
21
NSString * binaryStringFromInteger( int number )
{
    NSMutableString * string = [[NSMutableString alloc] init];

    int spacing = pow( 2, 3 );
    int width = ( sizeof( number ) ) * spacing;
    int binaryDigit = 0;
    int integer = number;

    while( binaryDigit < width )
    {
        binaryDigit++;

        [string insertString:( (integer & 1) ? @"1" : @"0" )atIndex:0];

        if( binaryDigit % spacing == 0 && binaryDigit != width )
        {
            [string insertString:@" " atIndex:0];
        }

        integer = integer >> 1;
    }

    return string;
}

I started from Adam Rosenfield's version, and modified to:

  • add spaces between bytes
  • handle signed integers

Sample output:

-7            11111111 11111111 11111111 11111001
7             00000000 00000000 00000000 00000111
-1            11111111 11111111 11111111 11111111
2147483647    01111111 11111111 11111111 11111111
-2147483648   10000000 00000000 00000000 00000000
0             00000000 00000000 00000000 00000000
2             00000000 00000000 00000000 00000010
-2            11111111 11111111 11111111 11111110

Comments

4

Roughly:

-(void)someFunction
{
  NSLog([self toBinary:input]);
}

-(NSString *)toBinary:(NSInteger)input
{
  if (input == 1 || input == 0) {
    return [NSString stringWithFormat:@"%d", input];
  }
  else {
    return [NSString stringWithFormat:@"%@%d", [self toBinary:input / 2], input % 2];
  }
}

6 Comments

This fails if the input is 0 (or in fact if the last bit is 0), and it also allocates and deallocates N string objects, where N is the number of bits in the number. Not very efficient.
NSMutableString is derived from NSString, so insertString probably does the same thing as this.
Except it doesn't. insertString:atIndex: inserts into the same buffer (and expands the buffer if necessary), whereas stringWithFormat allocates a new autoreleased NSString object.
Either way, it's a minor bit of throwaway code. The hit from doing it with NSMutableString vs. grabbing a new NSString is negligible enough. Not to mention that I did it recursively, which eats up stack space as well. The best that could be said is that it is functional (as in style of programming).
Thanks to all for the suggestions/comments.
|

Your Answer

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