Pages

Convert NSData to NSString

Below example explains how to convert NSData to NSString with Objective-C.

using -[NSString initWithData] function we can convert NSData to NSString. This function returns nil if the encoding is not correct.
Syntax of initWithData

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;



NSString * str =@"Hello";
NSData * data =[str dataUsingEncoding:NSUTF8StringEncoding]; //Data
//Convert NSData to NSString
NSString * converted =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data =%@",converted);