Objective-C Convert NSData to NSString - iphone

Here are the code I tried to convert NSData to NSString but the program return "Program received signal:SIGABRT".
NSString *string= [NSString stringWithUTF8String:[data bytes]];
OR
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Is there any other better way to do it?

Here's a highly up-voted answer that shows how to do it. In a nutshell:
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
The first idea looks like a fail because of sending [data bytes]. stringWithUTF8String isn't prepared for a void *. The second idea looks as if it should work, even with nil input.

Use this code
NSString* myString;
myString = [[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];
also see this tutorial..
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
:)

Swift-3
let string = data.base64EncodedString()

Related

Converting NSData to NSString

I am parsing a URL, I am getting some data in a NSMutableData object i.e urlData. Now in - (void)connectionDidFinishLoading:(NSURLConnection *)connection {} I would like to convert the urlData to a NSString. I have tried with:
NSString *responseData = [NSString stringWithCString:[urlData bytes] length:[urlData length]];
NSString* responseData = [NSString stringWithUTF8String:[urlData bytes]];
NSString *responseData = [[NSString alloc] initWithBytes:[urlData bytes] length:[urlData length] encoding:NSUTF8StringEncoding] ;
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
I am either getting null in NSString object or PK(reverse Question Marks which i am unable to show you).
Can anyone give me a solution for this?
-(id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
is the method you need to use. Your problem is either that you are not using the correct encoding or the data is not string data. NSLog the data and examine it to figure pout what it really is.
From the Apple documents:
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).
Use below code it will work
NSString *str=[[NSString alloc] initWithBytes:[urlData bytes] length:[urlData length] encoding:NSStringEncodingConversionAllowLossy];
NSLog(#"urlData........%#",str);

Converting NSString to NSData and vice versa

I am having an issue while trying to convert NSString to NSData and vice versa.
I am trying to store encrypted string to my database. For that I am using AES algorithm. Now what I am doing is I get encrypted NSData and I am converting this to NSString using following:
// Not woking
NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSUTF8StringEncoding];
// Working
NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSASCIIStringEncoding];
Why NSData is not converting while using NSUTF8StringEncoding. Same way when I try to convert the string got by NSASCIIStringEncoding using:
// Not working
[strTemp dataUsingEncoding:NSASCIIStringEncoding];
// Working
[strTemp dataUsingEncoding:NSUTF8StringEncoding];
Why NSASCIIStringEncoding is not working while converting the NSString to NSData?
NSString to NSData:
NSString* str= #"teststring";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData to NSString:
NSString* newStr = [[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding];
Please mind that NSASCIIStringEncoding and NSUTF8StringEncoding need to match the characters in the string.

problem in converting NSString to NSData

I want to convert one NSString to NSData. on Using encoding am getting different value in NSData by displaying it using its description property.
I have
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
on printing [data description]; am getting some different values...
how to extract the same string from the NSData.. pl help me out on this problem.
Create a new NSString:
NSString *newString = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
You can use NSString's initWithData:encoding: method:
NSString *str=#"80369F4";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *sameString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

How to use method stringWithContentsOfFile:encoding:error

I don't know how to use this method.
I want to get the data in an rtf file as a NSString, however I can not.
Here is part of my code.
NSString *filePath = #"path of the file";
NSString *ImagePath = [[[NSString alloc] init] autorelease];
imagePath = [NSString stringWithContentsOfFile: filePath encoding:(NSStringEncoding)nil error:(NSError**) nil];
I can't readout the data in that .rtf file. I am appreciate that everyone may help me. Thanks.
Try using this
imagePath = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil];
Here is the function
NSString *content = [[[NSString alloc] initWithContentsOfFile:filepath
usedEncoding:nil
error:nil] autorelease];
before using this be sure u giving a right path (filepath) to the string.

How to convert byte array to NSString

I am reading data from a TCP/IP stream and am successfully receiving a byte array from the pre-existing server. I am now trying to find a way to convert that array to an NSString. I have found several examples, but am having a hard time getting my desired results.
NSData *data=[[NSMutableData alloc] init];
uint8_t buffer[1024];
unsigned int len=0;
len=[(NSInputStream *)stream read:buffer maxLength:1024];
if(len>0){
[data appendBytes:&buffer length:len];
//BYTE ARRAY OBTAINED OK!!
///////////////////////////////////////////////////////
//METHOD #1 - Yields 'nil'
NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
///////////////////////////////////////////////////////
//METHOD #2 - Log prints OK, but messageString says
//'invalid' in debugger, and get warnings all over the
//place. I know this is wrong, but it semi-works :)
size_t length=[data length];
unsigned char aBuffer[length];
[data getBytes:aBuffer length:length];
aBuffer[length - 1]=0;
NSString *messageString =aBuffer;
NSLog (#"%s",messageString);
///////////////////////////////////////////////////////
}else{
NSLog(#"No Buffer");
}
Please help! Any assistance provided is GREATLY appreciated.
I got the answer.
I had to change this:
NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
To this:
NSString *string = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
This is wrong:
[data appendBytes:&buffer length:len];
It should be:
[data appendBytes:buffer length:len];
NSString* string = [NSString stringWithUTF8String: data];
Make sure your data is null-terminated, obviously.