iPhone: strange output when casting nsdata to nsstring - iphone

I get a NSData object in return after decrypting a payload with aes128:
NSData *returnData = [ciphertext AES128DecryptWithKey:keyData withIV:ivData];
I get the following hex output when i try to NSLog this:
<2db88b73 d84599a1 5779c736 09c975b7 92750cf2 d11cb41b 19f13781
4401bc57 b2ad96c8 402e3ccf 851c0219 00aec76b>
I then try to setting it as NSString:
[[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
When using NSLog() on the string i get "(null)" as output.
Can someone tell me why and where i should look for the problem?

Collided with the same issue some time ago, found the answer here.
If the data is not null-terminated, you should use -initWithData:encoding:
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
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 have ARC enabled, remove the -autorelease call.)

Related

convert emojis into unicode dynamically

How should i convert the emojis into unicode and send it to server and then can decode it dynamically.
I can statically give the if condition for perticular emojis.But i want to make it dynamically.
Thanks in advance
few days ago i have same problem. i tried this its work for me :
to pass value to the server
NSData *data = [textView.text dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
goodValue = [goodValue stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
when fetch value from the server
NSString *goodValue = [StaticClass urlDecode:[dataDict objectForKey:#"comment_text"]];
NSData *newdata=[goodValue dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *mystring=[[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];

How to Convert NSData to NSString

I know this has been asked quite before, and I already followed couple of approaches, but they don't work.
Following is what I already tried:
NSString *newStr = [NSString stringWithUTF8String:[responseData bytes]];
NSString *newStr = [NSString stringWithFormat:#"%.*s", [responseData length], [responseData bytes]];
None of them works. In 1st case, it fills newStr with null. In 2nd, it fills with junk characters. I know from debugger log (po responseData) that I get valid response which is like bbbbbb 00 bbbbbb. [server sends them as byte array]
What to do?
EDIT:
I am receiving this data from http request response - using ASIHTTPRequest library, in case anybody can help on that line.
Try this,
NSData *responseData; [Initialize it]
NSString *receivedDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#",receivedDataString);
Please try following code
NSString *string = [[[NSString alloc] initWithData: responseData.bytes encoding:NSUTF8StringEncoding] autorelease];
You can use this code lines
NSString *str=[[NSString alloc] initWithBytes:data1.bytes length:data1.length encoding:NSUTF8StringEncoding];
I am posting this for records sake because I found a duplicate and voting to close this down.
Actually what I am receiving is a stream of bytes represented as hex, and all the answers indicated do not work. Only [NSData description] gave me true data, which is something I can't use because it is intended for debugging.
Finally I tried the solution given here, and I get what I want.
Thanks to all for trying to help out.
NSString *image1Data = [[NSData dataWithData:myData] encodeBase64ForData];
But for this, you have to use NSData+Base64Additions class.
Use following way
NSString *dC_Str = [[NSString alloc] initWithData:decryPtd_data encoding:NSASCIIStringEncoding] ;

passing a UInt32 into a NSData object returns (null)

I am trying to pass a NSNumber into a UInt32, which is working.. Then I am trying to stuff that UInt32 into a NSData object.. however this is where things get abit funky...
when I try to write whats in that NSData object out to a string its returning (null).
This is what my code looks like, I think it has something to do with the way I am passing CACHE_VALUE into requestCacheData.. but I am not totally sure why.
// Use the correctly returned cache number
UInt32 CACHE_VALUE = [cacheNumber intValue];
NSLog(#"%lu", CACHE_VALUE); //gives me the correct integervalue
NSData * requestCacheData = [[NSData alloc] initWithBytes:&CACHE_VALUE length:sizeof(CACHE_VALUE)];
NSString *myString = [[NSString alloc] initWithData:requestCacheData encoding:NSUTF8StringEncoding];
NSLog(#"%#", myString); //outputs (null)
any help would be appreciated.
you can't turn the NSData to a right NSString that NSData do not contain a right string content.
in your case , your NSData just contain a binary unsigned int,
what do you suppose to get ?
NSString* str= #"a NSString";
NSData* requestCacheData =[str dataUsingEncoding:NSUTF8StringEncoding];
NSString *myString = [[NSString alloc] initWithData:requestCacheData encoding:NSUTF8StringEncoding];
NSLog(#"%#", myString);
you may get the right ouput,
but if you want to turn a UINT to NSString
just
NSString *str = [[[NSString alloc] initWithFormat:#"%u",CACHE_VALUE] autorelease];

Memory leak when parse JSON string using SBJSON

I am using following code to parse JSON and getting memory leak (100%) on line number 2. I don't know what is the problem, can someone help me ?
NSString *response = [request responseString];
NSMutableDictionary *responseJSON = [response JSONValue]; (100% leak)
NSString *tockenString = [responseJSON objectForKey:#"Token"];
NSString *userIDString = [responseJSON objectForKey:#"ID"];
I found the answer. Go to SBJsonParser.m function scanRestOfString and change the line
from
*o = [[NSMutableString alloc] initWithBytes:(char*)c length:len encoding:NSUTF8StringEncoding];
to
*o = [[[NSMutableString alloc] initWithBytes:(char*)c length:len encoding:NSUTF8StringEncoding] autorelease];
SBJsonParser has a like in scanRestOfString/NSMutableString. I reported the bug as well. Thank you all.
Remove those 2 autoreleases. They overrelease the objects.
I've had a similar problem but it turned out the leak was actually higher in the chain of methods, but Instruments was (mistakenly) pointing to this line. Look at the methods that store the results retrieved from this code.

Xcode IPhone Parse Server Output

I am sending some data to the server that is coming back as a string, but i need to parse it at the carriage return (new line)
The variable the output is being stored in is
NSString *serverOutput = [[NSString alloc] initWithData: dataurl encoding:NSASCIIStringEncoding];
The output is:
1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2
Any suggestions or help?
Something like this:
NSString *serverOutput = #"1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2";
NSArray *myData = [serverOutput componentsSeparatedByString:#"\n"];