Problem converting NSData to NSString

Status
Not open for further replies.

ScottSwing

Ars Scholae Palatinae
807
I'm trying to convert an NSData object to an NSString using:

Code:
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
or
Code:
[NSString stringWithUTF8String:[data bytes]];

Both of these return nil using the iOS 5.1 simulator. On iOS 6, they work fine. The data isn't nil. I don't have an iOS 5 device to use for testing, so how can I make sure I'm doing this right? Using Xcode 4.5.1.
 

Catfish

Ars Tribunus Angusticlavius
9,954
That would suggest the data isn't a valid UTF8 string. What does it look like?

I don't have an iOS 5 device to use for testing, so how can I make sure I'm doing this right?

Honestly? You can't. It's a shitty situation, but you do kinda need an actual device for each major OS version you want to support, at least.

... that said, I'd be mildly surprised if there is a difference in that method between the simulator and the actual OS. I've only discovered discrepancies in things like audio hardware interfacing and OpenGL.
 

Ember

Ars Centurion
346
Subscriptor++
topham":xoqbosdn said:
It's far more likely you have an allocation issue that's different between the versions, rather than the underlying API giving different results.
++
I think the 'data' is deallocated already. Maybe the buffer is overridden by other objects on iOS 5 simulator making the content invalid UTF-8 string, but intact on iOS 6 runtime.

And I personally prefer -initWithData:encoding: as I don't want to deal with internal buffer of NSData and C pointers. An added bonus is that it works even when the content is not NULL terminated (I presume, from the documentation).
 

JamesW

Ars Tribunus Militum
1,946
Subscriptor
LordHunter317":3h0kb71e said:
So either the ASCII encoding is very leinent, the UTF-8 is uselessly broken, or you still haven't truly fixed the problem.
It's the former. NSASCIIStringEncoding claims: "Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0…127 only.", but it accepts anything.
Code:
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {       
        char ascii[] = {72,101,108,108,111,0};
        char notAscii[] = {87,111,114,108,100,32,58,222,0};
        
        NSString* asciiString = [NSString stringWithCString:ascii encoding:NSASCIIStringEncoding];
        NSString* notAsciiString = [NSString stringWithCString:notAscii encoding:NSASCIIStringEncoding];
        
        NSLog(@"%@", asciiString);
        NSLog(@"%@", notAsciiString)
    }
    return 0;
}

2012-11-15 17:51:44.552 Ascii[31289] Hello
2012-11-15 17:51:44.554 Ascii[31289] World :Þ
 
You probably shouldn't rely on that behavior. It looks like NSASCIIStringEncoding is behaving as NSISOLatin1StringEncoding is defined to behave, so if nothing else you should switch to that.

Alternatively, you should fix whatever code is creating the NSData so that it's using the encoding that it should be using in the first place. Fixing mangled data downstream is not a desirable thing, and doing so just breeds tolerance for generating more bad data.
 
Status
Not open for further replies.