Thursday, April 21, 2011

NSString to Print in in binary format

Hi 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

From stackoverflow
  • 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.

    kperryua : I'd have to see the code you used when you tried it to help you any more.
  • Hi kperry NSString *str = [ [NSString alloc] initWithBytes:barr length:sizeof(barr) encoding:NSUTF8StringEncoding]];

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.