1

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

1
  • obj-c code is uint8 barr[4]; barr[0] =(uint8)num; barr[1] =(uint8)num>>8; barr[2] =(uint8)num>>16; barr[3] =(uint8)num>>24; i am using [NSString stringwithFormat:@%i,barr[i]] which %? i need to use to get the byte value(binary data) as it is from the array? Commented Aug 17, 2009 at 6:40

6 Answers 6

5

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

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

Comments

4

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;
}

Comments

2

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

Comments

1

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.

2 Comments

Hi kperry I have tried but i am not getting the byte value copied into NSString , any idea?
I'd have to see the code you used when you tried it to help you any more.
1
-(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;
 }
}

Comments

0

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.

Comments

Your Answer

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