6

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.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89

6 Answers6

13

Try this,

NSData *responseData; [Initialize it]
NSString *receivedDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",receivedDataString);
vinothp
  • 9,939
  • 19
  • 61
  • 103
3

Please try following code

NSString *string = [[[NSString alloc] initWithData: responseData.bytes encoding:NSUTF8StringEncoding] autorelease];
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
3

You can use this code lines

NSString *str=[[NSString alloc] initWithBytes:data1.bytes length:data1.length encoding:NSUTF8StringEncoding];
CReaTuS
  • 2,593
  • 1
  • 19
  • 31
aknew
  • 1,101
  • 7
  • 23
  • what are your data? I get this string from working project and its work well. May be you data encoding isn't UTF8? – aknew Jan 22 '13 at 11:42
3

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.

Community
  • 1
  • 1
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
2
NSString  *image1Data = [[NSData dataWithData:myData] encodeBase64ForData];

But for this, you have to use NSData+Base64Additions class.

Rahul Gupta
  • 808
  • 11
  • 15
1

Use following way

 NSString *dC_Str = [[NSString alloc] initWithData:decryPtd_data encoding:NSASCIIStringEncoding] ;
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
Nag Raj
  • 880
  • 1
  • 12
  • 18