NSString以二进制格式打印

时间:2009-08-17 06:24:59

标签: objective-c

我不明白下面将int转换为byte的情况,下面的java代码可以打印如下的字节值

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

二进制输出::: 0
二进制输出::: 0
二进制输出::: 0
二进制输出::: 1
二进制输出::: squaresquaresquaresquare(4平方字符) - 二进制数据

但是当我将相同数据的目标c代码制作成最终的NSString时,它也打印为“0001”但不是二进制格式(4个方形字符)

我需要二进制格式的NSString如何以二进制格式而不是“0001”打印NSString

请帮助

6 个答案:

答案 0 :(得分:5)

如果可读性是一个问题,那么让我建议一个快速例程,它将把一个int转换成一串半字节大小的“1”和“0”字符组:

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

那样 binaryRepresentation(1)返回0000 0000 0000 0000 0000 0000 0000 0001

binaryRepresentation(1423)返回0000 0000 0000 0000 0000 0101 1000 1111

答案 1 :(得分:4)

你可以使用这个功能

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

答案 2 :(得分:2)

<强>代码

@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

正在使用

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

<强>输出

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

答案 3 :(得分:1)

字符串正在打印整数,因为这是你要放入的内容(%i ==整数)。 %c是字符的标记。

或者,您可以将数组传递到-[NSString initWithBytes:length:encoding:]。如果你是一个包含单个字节的字符串,请使用相同的方法,将偏移量指针传递给数组,长度为1。

答案 4 :(得分: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;
 }
}

答案 5 :(得分:0)

请使用以下代码

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

我也尝试过ASCII编码,但它只提供空字符串。