Monday, June 11, 2012

Convert UTF-8 encoded NSData to NSString


I have UTF-8 encoded nsdata from windows server. I want to convert it to nsstring for iphone. Since data contains characters(like degree symbol) which have different values on both platforms, how do I convert data to string.



Source: Tips4all

2 comments:

  1. NSString* newStr = [[[NSString alloc] initWithData:theData
    encoding:NSUTF8StringEncoding] autorelease];


    If the data is null-terminated, you should instead use

    NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];

    ReplyDelete
  2. You could call + (id)stringWithUTF8String:(const char *)bytes.

    ReplyDelete