I’m trying to use the BEncoding ObjC class to decode a .torrent file.
NSData *rawdata = [NSData dataWithContentsOfFile:@'/path/to/the.torrent']; NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
When I NSLog torrent I get the following:
{ announce = <68747470 3a2f2f74 6f727265 6e742e75 62756e74 752e636f 6d3a3639 36392f61 6e6e6f75 6e6365>; comment = <5562756e 74752043 44207265 6c656173 65732e75 62756e74 752e636f 6d>; 'creation date' = 1225365524; info = { length = 732766208; name = <7562756e 74752d38 2e31302d 6465736b 746f702d 69333836 2e69736f>; 'piece length' = 524288; ....
How do I convert the name into a NSString? I have tried..
NSData *info = [torrent valueForKey:@'info']; NSData *name = [info valueForKey:@'name']; unsigned char aBuffer[[name length]]; [name getBytes:aBuffer length:[name length]]; NSLog(@'File name: %s', aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:
File name: ubuntu-8.10-desktop-i386.iso)
I have also tried (from here)..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
..but this seems to return a bunch of random characters:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
The fact the first way (as mentioned in the Apple documentation) returns most of the data correctly, with some additional bytes makes me think it might be an error in the BEncoding library.. but my lack of knowledge about ObjC is more likely to be at fault..
That would be an NSDictionary, then, not an NSData.
No, it retrieved the filename just fine; you simply printed it incorrectly.
%stakes a C string, which is null-terminated; the bytes of a data object are not null-terminated (they are just bytes, not necessarily characters in any encoding, and 0—which is null as a character—is a perfectly valid byte). You would have to allocate one more character, and set the last one in the array to 0:But null-terminating the data in an NSData object is wrong (except when you really do need a C string). I’ll get to the right way in a moment.
That’s because your bytes are UTF-8, which encodes one character in (usually) one byte.
unicharis, andstringWithCharacters:length:accepts, UTF-16. In that encoding, one character is (usually) two bytes. (Hence the division bysizeof(unichar): it divides the number of bytes by 2 to get the number of characters.)So you said “here’s some UTF-16 data”, and it went and made characters from every two bytes; each pair of bytes was supposed to be two characters, not one, so you got garbage (which turned out to be mostly CJK ideographs).
You answered your own question pretty well, except that
stringWithUTF8String:is simpler thanstringWithCString:encoding:for UTF-8-encoded strings.However, when you have the length (as you do when you have an NSData), it is even easier—and more proper—to use
initWithBytes:length:encoding:. It’s easier because it does not require null-terminated data; it simply uses the length you already have. (Don’t forget to release or autorelease it.)