How to convert NSData to NSString

I know that this was asked a long time ago, and I have already followed several approaches, but they do not work.

Here is what I have already tried:

NSString *newStr = [NSString stringWithUTF8String:[responseData bytes]]; NSString *newStr = [NSString stringWithFormat:@"%.*s", [responseData length], [responseData bytes]]; 

None of them work. In the first case, it fills newStr with a null value. In the 2nd, it is filled with unnecessary characters. I know from the debugger log (po responseData) that I am getting a valid response that looks like bbbbbb 00 bbbbbb. [server sends them as an array of bytes]

What to do?

EDIT: I get this data from an HTTP request response - using the ASIHTTPRequest library, in case someone can help on this line.

+4
source share
6 answers

I post this for entries because I found a duplicate and a vote to close it.

In fact, what I get is a stream of bytes represented as hex, and all of the answers given do not work. Only [NSData's description] gave me true data, which I cannot use because it is intended for debugging.

Finally, I tried the solution given here and I get what I want. Thanks to everyone for trying to help.

+3
source

Try it,

 NSData *responseData; [Initialize it] NSString *receivedDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"%@",receivedDataString); 
+10
source

You can use these lines of code

 NSString *str=[[NSString alloc] initWithBytes:data1.bytes length:data1.length encoding:NSUTF8StringEncoding]; 
+3
source

Please try the following code

 NSString *string = [[[NSString alloc] initWithData: responseData.bytes encoding:NSUTF8StringEncoding] autorelease]; 
+2
source
 NSString *image1Data = [[NSData dataWithData:myData] encodeBase64ForData]; 

But for this you need to use the NSData + Base64Additions class.

+1
source

Use the following method

  NSString *dC_Str = [[NSString alloc] initWithData:decryPtd_data encoding:NSASCIIStringEncoding] ; 
0
source

All Articles