Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 136863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:00:56+00:00 2026-05-11T07:00:56+00:00

I’m trying to use the BEncoding ObjC class to decode a .torrent file. NSData

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:00:57+00:00Added an answer on May 11, 2026 at 7:00 am
    NSData *torrent = [BEncoding objectFromEncodedData:rawdata]; 

    When I NSLog torrent I get the following:

    {     ⋮ } 

    That would be an NSDictionary, then, not an NSData.

    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) 

    No, it retrieved the filename just fine; you simply printed it incorrectly. %s takes 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:

    size_t length = [name length] + 1; unsigned char aBuffer[length]; [name getBytes:aBuffer length:length]; aBuffer[length - 1] = 0; NSLog(@'File name: %s', aBuffer); 

    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.

    I have also tried […]..

    NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)]; 

    ..but this seems to return random Chinese characters:

    扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳 

    That’s because your bytes are UTF-8, which encodes one character in (usually) one byte.

    unichar is, and stringWithCharacters:length: accepts, UTF-16. In that encoding, one character is (usually) two bytes. (Hence the division by sizeof(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 than stringWithCString: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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 143k
  • Answers 143k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sure. Whereas C-w by default performs a (kill-region) you could… May 12, 2026 at 8:31 am
  • Editorial Team
    Editorial Team added an answer This class is created in order to provide you with… May 12, 2026 at 8:31 am
  • Editorial Team
    Editorial Team added an answer You could implement your own Comparer like this: public class… May 12, 2026 at 8:31 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.