我想用BEncoding ObjC class来解码.torrent
文件.
NSData *rawdata = [NSData dataWithContentsOfFile:@"/path/to/the.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
当我NSLog
torrent
时,我得到以下信息:
{
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;
....
如何将name
转换为NSString?我试过了..
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);
…它会重新检索数据,但之后似乎有额外的unicode垃圾:
File name: ubuntu-8.10-desktop-i386.iso)
我也试过(from here)..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
但这似乎返回了一堆随机字符:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
第一种方法(如Apple文档中所述)正确返回了大部分数据,并添加了一些字节,这一事实让我认为这可能是BEncoding库中的一个错误..但我对ObjC缺乏了解更可能是我的错..