bs
bs

Reputation:

NSString to Print in in binary format

I dont understand of the below case of converting int to byte , the below java code can able to print the byte value as below

System.out.println("binary output ::: "+Byte.toString(bo[0]));
System.out.println("binary output ::: "+Byte.valueOf(bo[1]));
System.out.println("binary output ::: "+Byte.valueOf(bo[2]));
System.out.println("binary output ::: "+Byte.valueOf(bo[3]));
System.out.println("binary output ::: "+new String(bo));

binary output ::: 0
binary output ::: 0
binary output ::: 0
binary output ::: 1
binary output ::: squaresquaresquaresquare ( 4 square chars) - binary data

but when i make a objective-c code to the same data into final NSString it also prints as "0001" but not in binary format ( 4 square chars)

I need NSString in binary format how do i print NSString in binary format instead of "0001"

please help

Upvotes: 1

Views: 8120

Answers (6)

Goman
Goman

Reputation: 11

-(NSString *)toBinary:(NSInteger)input
{
 if (input == 1 || input == 0) {
 return [NSString stringWithFormat:@"%.8d", input];
 }
 else
 {
 NSString * myString = [NSString stringWithFormat:@"%@%d", [self toBinary:input / 2], input %    2];
 NSRange stringRange = {[myString length] - 8,8};
 NSString *shortString = [myString substringWithRange:stringRange];
 return shortString;
 }
}

Upvotes: 1

hfossli
hfossli

Reputation: 22962

Code

@interface NSString (BinaryStringRepresentation)

+ (NSString *)binaryStringRepresentationOfInt:(long)value;
+ (NSString *)binaryStringRepresentationOfInt:(long)value numberOfDigits:(unsigned int)length chunkLength:(unsigned int)chunkLength;

@end

@implementation NSString (BinaryStringRepresentation)

+ (NSString *)binaryStringRepresentationOfInt:(long)value
{
    const unsigned int chunkLength = 4;
    unsigned int numberOfDigits = 8;
    return [self binaryStringRepresentationOfInt:value numberOfDigits:numberOfDigits chunkLength:4];
}

+ (NSString *)binaryStringRepresentationOfInt:(long)value numberOfDigits:(unsigned int)length chunkLength:(unsigned int)chunkLength
{
    NSMutableString *string = [NSMutableString new];

    for(int i = 0; i < length; i ++) {
        NSString *divider = i % chunkLength == chunkLength-1 ? @" " : @"";
        NSString *part = [NSString stringWithFormat:@"%@%i", divider, value & (1 << i) ? 1 : 0];
        [string insertString:part atIndex:0];
    }

    return string;
}

@end

In use

NSLog(@"%@", [NSString binaryStringRepresentationOfInt:0]);
NSLog(@"%@", [NSString binaryStringRepresentationOfInt:1]);
NSLog(@"%@", [NSString binaryStringRepresentationOfInt:2]);
NSLog(@"%@", [NSString binaryStringRepresentationOfInt:3]);
NSLog(@"%@", [NSString binaryStringRepresentationOfInt:7]);
NSLog(@"%@", [NSString binaryStringRepresentationOfInt:16]);

Outputs

0000 0000
0000 0001
0000 0010
0000 0011
0000 0111
0001 0000

Upvotes: 2

bs
bs

Reputation:

Please use following code

NSString *str =  [ [NSString alloc] initWithBytes:barr length:sizeof(barr) encoding:NSUTF8StringEncoding]];

I have tried ASCII encoding also , but it gives empty string only.

Upvotes: 0

Robert Altman
Robert Altman

Reputation: 5505

If readability is an issue, let me suggest a quick routine which will turn an int into a string of nibble-sized groupings of "1" and "0" characters:

NSString *binaryRepresentation(int value)
{
    long nibbleCount = sizeof(value) * 2;
    NSMutableString *bitString = [NSMutableString stringWithCapacity:nibbleCount * 5];

    for (int index = 4 * nibbleCount - 1; index >= 0; index--)
    {
        [bitString appendFormat:@"%i", value & (1 << index) ? 1 : 0];
        if (index % 4 == 0)
        {
            [bitString appendString:@" "];
        }
    }

    return bitString;
}

So that
binaryRepresentation(1) returns 0000 0000 0000 0000 0000 0000 0000 0001
and
binaryRepresentation(1423) returns 0000 0000 0000 0000 0000 0101 1000 1111

Upvotes: 5

hfossli
hfossli

Reputation: 22962

you could use this function

+ (NSString *)getBitStringForInt:(int)value {

    NSString *bits = @"";

    for(int i = 0; i < 8; i ++) {
        bits = [NSString stringWithFormat:@"%i%@", value & (1 << i) ? 1 : 0, bits];
    }

    return bits;
}

Upvotes: 4

kperryua
kperryua

Reputation: 10534

The string is printing the integers, because that's what you're putting into it (%i == integer). %c is the token for characters.

Or, you can just pass the array into -[NSString initWithBytes:length:encoding:]. If you a string with a single byte, use the same method, passing an offset pointer into the array, and a length of one.

Upvotes: 1

Related Questions