Converting NSData to NSString in Objective c

IphoneObjective CIosIpad

Iphone Problem Overview


I want to convert NSData to NSString..What is the best way to do this?

I am using this code but the final string returns null

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);

When I see console It will print null.

Iphone Solutions


Solution 1 - Iphone

Use below code.

NSString* myString;
myString = [[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];

Solution 2 - Iphone

The docs for NSString says

https://developer.apple.com/documentation/foundation/nsstring/1416374-initwithdata

> Return Value An NSString object > initialized by converting the bytes in > data into Unicode characters using > encoding. The returned object may be > different from the original receiver. > Returns nil if the initialization > fails for some reason (for example if > data does not represent valid data for > encoding).

You should try other encoding to check if it solves your problem

 // The following constants are provided by NSString as possible string encodings.
enum {
   NSASCIIStringEncoding = 1,
   NSNEXTSTEPStringEncoding = 2,
   NSJapaneseEUCStringEncoding = 3,
   NSUTF8StringEncoding = 4,
   NSISOLatin1StringEncoding = 5,
   NSSymbolStringEncoding = 6,
   NSNonLossyASCIIStringEncoding = 7,
   NSShiftJISStringEncoding = 8,
   NSISOLatin2StringEncoding = 9,
   NSUnicodeStringEncoding = 10,
   NSWindowsCP1251StringEncoding = 11,
   NSWindowsCP1252StringEncoding = 12,
   NSWindowsCP1253StringEncoding = 13,
   NSWindowsCP1254StringEncoding = 14,
   NSWindowsCP1250StringEncoding = 15,
   NSISO2022JPStringEncoding = 21,
   NSMacOSRomanStringEncoding = 30,
   NSUTF16StringEncoding = NSUnicodeStringEncoding,
   NSUTF16BigEndianStringEncoding = 0x90000100,
   NSUTF16LittleEndianStringEncoding = 0x94000100,
   NSUTF32StringEncoding = 0x8c000100,
   NSUTF32BigEndianStringEncoding = 0x98000100,
   NSUTF32LittleEndianStringEncoding = 0x9c000100,
   NSProprietaryStringEncoding = 65536
};

Solution 3 - Iphone

-[NSString initWithData:encoding] will return nil if the specified encoding doesn't match the data's encoding.

Make sure your data is encoded in UTF-8 (or change NSUTF8StringEncoding to whatever encoding that's appropriate for the data).

Solution 4 - Iphone

NSString *string = [NSString stringWithUTF8String:[Data bytes]];

Solution 5 - Iphone

Objective C:

[[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];

Swift:

let str = String(data: data, encoding: .ascii)

Solution 6 - Iphone

-[NSString initWithData:encoding] is your friend but only when you use a proper encoding.

Solution 7 - Iphone

Swift:

let jsonString = String(data: jsonData, encoding: .ascii)

or .utf8 or whatever encoding appropriate

Solution 8 - Iphone

Unsure of data's encoding type? No problem!

Without need to know potential encoding types, in which wrong encoding types will give you nil/null, this should cover all your bases:

NSString *dataString = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];

Done!




Note: In the event this somehow fails, you can unpack your NSData with NSKeyedUnarchiver , then repack the (id)unpacked again via NSKeyedArchiver, and that NSData form should be base64 encodeable.

id unpacked = [NSKeyedUnarchiver unarchiveObjectWithData:data];
data = [NSKeyedArchiver archivedDataWithRootObject:unpacked];

Solution 9 - Iphone

Objective C includes a built-in way to detect a the encoding of a string embedded in NSData.

NSData* data = // Assign your NSData object...

NSString* string;
NSStringEncoding encoding = [NSString stringEncodingForData:data encodingOptions:nil convertedString:&string usedLossyConversion:nil];

Solution 10 - Iphone

> in objective C:

NSData *tmpData;
NSString *tmpString = [NSString stringWithFormat:@"%@", tmpData];
NSLog(tmpString)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMehul MistriView Question on Stackoverflow
Solution 1 - IphoneJhaliya - Praveen SharmaView Answer on Stackoverflow
Solution 2 - IphoneFelipe SabinoView Answer on Stackoverflow
Solution 3 - IphoneMorten FastView Answer on Stackoverflow
Solution 4 - IphoneNag RajView Answer on Stackoverflow
Solution 5 - IphoneAlexander VolkovView Answer on Stackoverflow
Solution 6 - IphoneBilal KhanView Answer on Stackoverflow
Solution 7 - IphoneJessView Answer on Stackoverflow
Solution 8 - IphoneAlbert RenshawView Answer on Stackoverflow
Solution 9 - IphoneAndrew RondeauView Answer on Stackoverflow
Solution 10 - IphoneAshok RView Answer on Stackoverflow