• Latest Code...

    Convert UTF-8 encoded NSData to NSString in ios

    UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms,  convert data to string following way.

    If the data is not null-terminated, you should use -initWithData:encoding:

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

    If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.

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

    if you are doing this, remember this: using "stringWithUTF8String:" on a string that is not null-terminated, the result is unpredictable.

    or simple call this for this

    +(id)stringWithUTF8String:(const char *)bytes.

    Happy Coding !!!

    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace