0

I'm having trouble converting binary data (some text) back to a string.

The original text comes from a JSON feed, and is then convertet into binary data to store (with the correct formatting) in CoreData. My problem is getting it back out of CoreData. When I try to convert it with the following: NSString *howToString = [[NSString alloc]initWithBytes:[dish.dishMainText bytes] length:[dish.dishMainText length] encoding:NSASCIIStringEncoding]; I get the following:

    bplist00‘T$topX$objectsX$versionY$archiver—TrootÄ¢   
U$nulloÚ1 kage



R¯r sukker, kakao og vanillekorn i sm¯rret. Pisk Êggene i et ad gangen, og tilsÊt til sidst melet. Fordel dejen i en smurt form p 24 cm. Bag kagen i ca. 20 minutter nederst i ovnen ved 175∞. 



Giv piskefl¯de og sm¯r et lille opkog. TilsÊt appelsinskal og chokolade, og lad den smelte ved lav temperatur. R¯r rundt i blandingen. HÊld chokoladeblandingen i en skÂl og sÊt den koldt. Fordel den faste tr¯ffelmasse ud over den afk¯lede kage.  



Server evt. kagen pyntet med blandede bÊr.܆_NSKeyedArchiver(25:<?E-2
D

As you can see, in the beginning and in the end there is some text that is not properly converted. I have tried different forms of encoding (UTF, Latin1/2 and so on) but nothing seems to work perfectly. This is the best result I have been able to produce so far, but it's obviously not satisfactory.

Let me know if more code is needed.

Thanks in advance Chris

Chris
  • 7,830
  • 6
  • 38
  • 72
  • How is it converted to binary data? – Daniel Jun 05 '14 at 11:44
  • I use RKEntityMapping (part of RestKit), and then just assign the JSON string to a Binary Data attribute of my entity – Chris Jun 05 '14 at 12:21
  • As @GregParker stated. This is an example of how to read it in Apple doc : https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/ReadWritePlistData/ReadWritePlistData.html#//apple_ref/doc/uid/10000048i-CH8-SW1 – 3329 Jun 05 '14 at 19:10

2 Answers2

0

That bplist00 text at the start of the data suggests that your data is in fact a binary plist, not a simple encoded string. Try using NSPropertyListSerialization to decode your data.

Greg Parker
  • 7,972
  • 2
  • 24
  • 21
  • 1
    Thanks for your answer Craig, I'm afraid that didn't give me the desired result either. It returns even more "raw data" – Chris Jun 05 '14 at 20:37
0

This is a keyed archive, which is a sort of subspecialty of property list. It's a serialized object graph from your app. You can unarchive it like this:

id rootObject = [NSKeyedUnarchiver unarchiveObjectWithData:[dish.dishMainText bytes]];

Note that the result -- assuming the data was archived from your app and it's a valid archive -- is an object graph, not a string.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204