title: iOS / Cocoa / Data interpretation with NSString initWithData to build a string drives me crazy
body: <p>I have a very simple callback called when a HTTP request has just been completed through a NSURLConnection object (connectionDidFinishLoading). This code just turns raw 7-bit encoded data read from a remote file into a NSString.</p>
  
  <pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
  NSString *string = [[NSString alloc] initWithData:mReceivedData encoding:NSASCIIStringEncoding];
  
  NSLog(@"string = %@", string);
  }
  </code></pre>
  
  <p>The issue is that string is NIL. I then suspected a bad conversion due to the presence of bad bytes (>0x80) but all my bytes read are pure 7-bit encoded ASCII data:</p>
  
  <pre><code>(gdb) po mReceivedData
  </code></pre>
  
  <p>&lt;2366696c 65566572 73696f6e 0a310a23 66696c65 54797065 0a626964 4465660a 23657865 72636973 654c6576 656c0a31 0a23636f 6c756d6e 54657874 0a537564 2f4f7565 73742f4e 6f72642f 4573740a 23636f6c 756d6e43 6f6c6f72 0a677265 656e2f72 65642f67 7265656e 2f726564 0a236269 6456616c 7565730a 2a2f3144 2f582f2d 0a3f2f2a 2f2a2f2a 0a2a2f2a 2f2a2f2a 0a2a2f2a 2f2a2f2a 0a236164 76696365 4c696e65 310a5370 6f75746e 696b2028 52c3a970 6f6e6461 6e740a23 61647669 63654c69 6e65320a 617072c3 a8732069 6e746572 76656e74 696f6e0a 2368616e 64436172 64730a4a 2f392f36 2f350a38 2f350a41 2f580a4b 2f372f36 2f332f32 0a237363 6f726547 7269640a 32532f31 300a3343 2f360a31 532f330a 32432f30 0a23616e 73776572 436f6d6d 656e7473 0a32532f 4f75693a 20646520 3820c3a0 20313020 706f696e 74732065 74203420 63617274 657320c3 a020532e 0a33432f 5072696f 726974c3 a920c3a0 206c6120 6d6f7965 6e6e652e 0a31532f 436f6d6d 65206176 6563207a c3a9726f 20706f69 6e74203f 0a32432f 4168206e 6f6e2021 0a236d61 696e436f 6d6d656e 740a4c65 20636f6d 6d656e74 61697265 20646520 4d696368 656c2042 65737369 732e0a0a 456e2072 c3a9706f 6e736520 61752063 6f6e7472 652064e2 80996170 70656c2c 20696c20 66617574 20646f6e 6e657220 6c652070 6c65696e 20646520 7361206d 61696e2e 20416e6e 6f6e6365 72203153 206d6f6e 74726572 61697420 64652030 20c3a020 3720706f 696e7473 202872c3 a9706f6e 73652066 6f7263c3 a965292e 0a0a4963 692c2069 6c206661 75742073 61757465 7220c3a0 2032532c 20656e63 68c3a872 65207175 69206e65 2070726f 6d657420 70617320 63696e71 20636172 74657320 65742071 75692064 c3a96372 69742075 6e206a65 75206465 203820c3 a0203130 20706f69 6e747320 482028c3 a0207061 72746972 20646520 31312070 6f696e74 732c2063 e2809965 73742075 6e206375 652d6269 64206f75 20756e20 73617574 20c3a020 6c61206d 616e6368 65207175 69207365 72612063 686f6973 69292e></p>
  
  <p>Those raw data are exactly the same as bytes contained into the remote file, so there are no polluting bytes.</p>
  
  <p>I also tried to play with UTF-8 conversion but this is still the same issue.</p>
  
  <p>I have another way of doing which would be to build a C string from those raw data and to build a NSString with something like NSStringWithCString... but I consider this is very ugly and I really would like to use the Cocoa API designed for such a purpose. There is no reason why I should not be able to use Cocoa power for such a basic task.</p>
  
  <p>Do I totally miss something ?</p>
  
  <p>Many thanks,
  Franz</p>