• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Converting between NSData and NSString losslessly
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Converting between NSData and NSString losslessly


  • Subject: Converting between NSData and NSString losslessly
  • From: email@hidden
  • Date: Sun, 24 Jul 2005 17:10:59 +1000

I'm in the unfortunate situation of having to parse someone else's poorly conceived file format (Bencode, used by BitTorrent, to be precise), which makes no distinction between actual human-readable strings and random binary data. The problems is in decoding - if I go for the lowest common denominator and make everything an NSData, I have to go to great pains to work with the data - e.g. I can no longer index into dictionaries using inline NSStrings. If I go for NSString's, I can't figure out a way to recover the original binary data perfectly...

... converting from NSData (the unparsed raw data) to NSString (as any encoding I've tried, including ASCII and UTF-8) and back to NSData results in the data being mutated. I've tested this with a simple app which fills an NSData instance with random bytes, then does the conversion there and back, and compares to two. No go; consistent failure. I know nothing about various string encodings, but this result doesn't really surprise me.

So, is there any way to convert "there and back again" without mutating the data?

There's also the further complication of what actual string encoding is used in the files... at present they're ASCII in practice, but I'd like to treat them as UTF-8 for future-proofing.

Wade Tregaskis (AIM/iChat, Yahoo & Skype: wadetregaskis, ICQ: 40056898, MSN: email@hidden, AV iChat & email: email@hidden, Jabber: email@hidden)
-- Sed quis custodiet ipsos custodes?


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Converting between NSData and NSString losslessly
      • From: email@hidden
  • Prev by Date: Re: Programatically created WebView crashes
  • Next by Date: Re: Converting between NSData and NSString losslessly
  • Previous by thread: Re: Programatically created WebView crashes
  • Next by thread: Re: Converting between NSData and NSString losslessly
  • Index(es):
    • Date
    • Thread