2012-05-19 10 views
5

私はリソースフォルダにバイナリファイル(file.bin)を持っています。私はそれを読んでバイナリとして表示したいと思います。その考え方はバイナリ情報を配列に入れることですが、最初はUILabelでそれを表示しようとしています:NSStringでNSStringをNSStringで表示する

`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に入れるにはどうすればいいですか?おかげさまで

+1

「バイナリ」とは、文字通り「ベース2表記」を意味しますか? – dasblinkenlight

+0

はい、そうです:NSSTRING string = @ "01000101000 .." – Sergiodiaz53

+0

あなた自身のバイナリデコーダを書く必要があります。私はバイナリリテラルとしてB "101010 .."を解釈する言語を一度書いたが、バイナリを読んだり書いたりする標準的な言語は見たことがない。 –

答えて

7

ネイティブがあるかどうかわかりませんが、回避策を提案できます。なぜあなたは変換を行う独自の機能をしないのですか?ここに私の例です:あなたは六角値を取得する代わりに

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

・ホープ、このことができます!

+0

それは動作します!どうもありがとう!!。私のファイルはバイナリで、変換する必要があるので変だ。 – Sergiodiaz53

+0

あなたのためにハッピーです:)正しい答えとして表示されるようにあなたの問題を解決した場合は、この回答を受け入れることを忘れないでください。 – antf

+0

GからZまではどうですか? – Supertecnoboff

関連する問題