I haven't seen a very clear answer on this one. For some reason I don't understand, the server I am receiving data from returns a JSON response with a lot (hundreds, maybe) of null bytes at the beginning.
When I use the following code, the string appears to be null:
NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];
This is apparently a common problem, but I haven't seen a clear answer on how to convert the NSData to an NSString.
Up until recently, I saw this problem with an intermediate NSString value, but it went away after I stripped a handful (but not hundreds) of characters off of the beginning and end. The NSString after stripping was perfectly fine, so I suppose the encoding method was not the problem.
Is there a built-in method that will do this properly? I have to imaging it would be more efficient that code I would write to go through byte-by-byte.
By the way, when I manually send the JSON request to the server in my browser, the response comes back as perfectly valid JSON, with no obvious problems showing up in the browser screen.
Any ideas what I should do? Thanks.