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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With