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.
Related
I have received some data in base64string format from JSON pasring in iphone. Now i want that data to store or decode in String. I have user Base64 class.
It return me null value. so pls help me!!!
I hope it will helpful to you
NSString *decodeString = #"Raja";
Encode String
NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
NSLog(#"Encode String Value: %#", base64String);
Decode String
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"Decode String Value: %#", decodedString);
Try to use like this..First you need to get this base64 library
https://github.com/l4u/NSData-Base64
And then import this file
#import "NSData+Base64.h"
NSData *data = [NSData dataFromBase64String:baseString];
NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
I hope it helps.
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);
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];
This is the string I need to post http://www.mysite.com/save?details={"name"}
Iam trying the following code;
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
NSString *hostStr = #"http://www.mysite.com/save?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
but the serverOutput always = null, unless i do just {name}. but i need the qutoes in there :(
You should convert everythign to valid HTTP symbols.
e.g.:
hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
...my guess is that your webserver is refusing your invalid URL.
wrong ..
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
Correct:
NSString *post =[NSString stringWithString:#"details={\"name\"}"];
NSData *myRequest = [NSString stringWithFormat:(#"&site=%#&key=%#",tmpSite,tmpKey)];
Why is this not working ?
Thanks
Problem is that you are assigning NSString instance to variable of NSData type. Try this:
NSString* s = [NSString stringWithFormat:#"&site=%#&key=%#",tmpSite,tmpKey];
NSData* d = [s dataUsingEncoding:NSASCIIStringEncoding]; //or any other encoding!
NSData *myRequest = [[NSString stringWithFormat:(#"&site=%#&key=%#",tmpSite,tmpKey)] dataUsingEncoding: NSASCIIStringEncoding];
You can't just mix the two classes like that.