-1

I'm attempting to convert a binary file into text, the problem is that a large portion of the file was not encoded in ascii and ends up being special characters. I'm using

[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

but am only getting a few characters back in a 20000 byte data block. What I would like to be able to see is all of the text (even if most is nonsense), which is what I get when I open the file using a binary editor.

enter image description here

nhgrif
  • 61,578
  • 25
  • 134
  • 173
davis
  • 1,911
  • 6
  • 26
  • 50
  • Pass the correct encoding? This sounds like an XY Problem. What are you trying to do with the string exactly? – nhgrif May 21 '15 at 00:08
  • There isn't really a 'correct encoding' for the entire file. Portions of the file [are readable with UTF8 or Ascii](http://i.imgur.com/OveTMs3.png) in a binary editor, but when I do a UTF8 or ascii encoding using the NSString methods I get very little or no data. I'd like to be able to view all of the characters to do some regex and pull out the pieces that make sense. – davis May 21 '15 at 00:12

1 Answers1

1

It's a binary file. To read it, you find the documentation for the file format, then you parse it. Trying to throw it all into an NSString* seems absolutely pointless.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • There are applications in reverse engineering, which is why hex viewers exist. Is there no way to view the contents of the file, [like this binary editor showing the file in UTF8 encoding?](http://i.imgur.com/OveTMs3.png) – davis May 21 '15 at 00:17
  • 1
    Of course. You'd use an encoding where every 8 bit character is valid. NSASCIIStringEncoding is no such encoding. UTF-8 is no such encoding. MacRoman or Windows1252 will do fine. – gnasher729 May 21 '15 at 00:19
  • Yep, using an arbitrary 8-bit encoding would do it. The other option is to loop through character-by-character, translating using a table you define. Fairly easy to do -- the hard part is defining the table. (Offhand, Windows1252 appears to match what's being displayed in the image above.) – Hot Licks May 21 '15 at 02:45