6

リソース フォルダーにバイナリ ファイル (file.bin) があり、それを読み取ってバイナリとして表示したいと考えています。バイナリ情報を配列に入れるというアイデアですが、最初は、次のように UILabel で表示しようとしています。

` NSData *databuffer; NSString *stringdata;

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"bin"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];

if (myData) {  
        stringdata = [NSString stringWithFormat:@"%@",[myData description]]; 
        labelfile.text = stringdata;
}  

`

ただし、データはHEXで表示されます。NSMutableArray に入れるためにバイナリで行うにはどうすればよいですか? ありがとう。

4

1 に答える 1

8

それを行うネイティブなものがあるかどうかはわかりませんが、回避策を提案できます。変換を行う独自の関数を作成してみませんか。これが私の例です:

Hex値を取得する場所:

NSString *str = @"Af01";
NSMutableString *binStr = [[NSMutableString alloc] init];

for(NSUInteger i=0; i<[str length]; i++)
{
    [binStr appendString:[self hexToBinary:[str characterAtIndex:i]]];
}
NSLog(@"Bin: %@", binStr);

回避策機能:

- (NSString *) hexToBinary:(unichar)myChar
{
    switch(myChar)
    {
        case '0': return @"0000";
        case '1': return @"0001";
        case '2': return @"0010";
        case '3': return @"0011";
        case '4': return @"0100";
        case '5': return @"0101";
        case '6': return @"0110";
        case '7': return @"0111";
        case '8': return @"1000";
        case '9': return @"1001";
        case 'a':
        case 'A': return @"1010";
        case 'b':
        case 'B': return @"1011";
        case 'c':
        case 'C': return @"1100";
        case 'd':
        case 'D': return @"1101";
        case 'e':
        case 'E': return @"1110";
        case 'f':
        case 'F': return @"1111";
    }
    return @"-1"; //means something went wrong, shouldn't reach here!
}

お役に立てれば!

于 2012-05-19T13:07:12.093 に答える