Printing NSData using NSLog

How to print the contents of an NSData object using NSLog:

-(void) post:(NSString*) msg to:(NSString*) link{ NSString *myRequestString = [NSString stringWithFormat:@"message=%@", msg]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: link]]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPMethod: @"POST"]; [request setHTTPBody: myRequestData]; NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSLog("%@", *returnData); //doesn't work } 

I would like to print the contents of * returnData ...

+46
objective-c iphone
Jul 08 2018-11-11T00:
source share
5 answers

If you do this:

 NSLog(@"%@", returnData); 

NSData will be registered in hexadecimal format. I think this is probably what you need.

If you want to turn it into a string and write a string, you first need to find out what character set was used. The default character set for HTTP is not UTF-8, it is ISO-8859-1 . One way to do this is to examine the Content-Type header for the charset section.

+56
Jul 08 '11 at 2:59 a.m.
source share

Convert NSData to NSString with

 NSString *strData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding]; 

and type NSString in NSLog as below

 NSLog(@"%@",strData); 

This answer is being edited for JeremyP since it does not know how to know that the content has UTF-8, although this was not a discussion of this issue.

You can get the response header in the following delegate method

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; NSDictionary *dic = [httpResponse allHeaderFields]; } 

This dictionary will give you all the header information as shown below.

 <CFBasicHash 0x5a45e40 [0x24b2380]>{type = immutable dict, count = 7, entries => 0 : <CFString 0x5d1bf60 [0x24b2380]>{contents = "X-Aspnet-Version"} = <CFString 0x5d21a60 [0x24b2380]>{contents = "2.0.50727"} 1 : <CFString 0x41a03a8 [0x24b2380]>{contents = "Server"} = <CFString 0x5d272f0 [0x24b2380]>{contents = "Microsoft-IIS/6.0"} 2 : <CFString 0x41a0010 [0x24b2380]>{contents = "Content-Length"} = <CFString 0x5d28630 [0x24b2380]>{contents = "385"} 6 : <CFString 0x419ff48 [0x24b2380]>{contents = "Cache-Control"} = <CFString 0x5d29c70 [0x24b2380]>{contents = "private, max-age=0"} 10 : <CFString 0x5d1c640 [0x24b2380]>{contents = "X-Powered-By"} = <CFString 0x5d26f10 [0x24b2380]>{contents = "ASP.NET"} 11 : <CFString 0x41a0060 [0x24b2380]>{contents = "Content-Type"} = <CFString 0x5d29c90 [0x24b2380]>{contents = "text/xml; charset=utf-8"} 12 : <CFString 0x41a0088 [0x24b2380]>{contents = "Date"} = <CFString 0x5d27610 [0x24b2380]>{contents = "Fri, 08 Jul 2011 15:23:10 GMT"} } 

Check charset = "utf-8", you will get the encoding from here.

+77
Jul 08 2018-11-11T00:
source share

You should also think:

 NSLog(@"%@", *returnData); // this is wrong. NSLog(@"%@", returnData); // this is correct. 

I hope I can help!

+4
Jan 28 2018-12-12T00:
source share

I often want to see what NSData actually represents. Usually this is some text that makes the sixth a little uncomfortable. Therefore, I usually write this piece in the JavaScript console in my web browser, it works quite quickly and can be easily changed if some further processing is required.

  • Copy / paste the following script into your browser console (right click here → Check item), press enter

     (function nsDataHexToString() { var str = prompt("Paste the hex string here:", "ié. 48656c6c 6f207468 657265...") var chs = str.replace(/[^A-F0-9]/ig,"").split("") var res = "" var cnt = 2 for (var i = 0; i+cnt-1<chs.length; i+=cnt) { var nr = "" for (var j=0; j<cnt; j++) nr += chs[i+j] nr = parseInt(nr, 16) res += String.fromCharCode(nr) } console.log(res) return res })() 
  • Run your swift / obj-c code, set a breakpoint and check your NSData object p>

     let sample = "Hello there" let data = sample.dataUsingEncoding(NSUTF8StringEncoding) // Put breakpoint here, hover over "data", and press the eye/i 
  • Copy the hexadecimal (something like <48656c6c 6f207468 657265> ) and paste it into the browser prompt

  • Then the console will display the line: "Hello there"

More recently, to check the output from NSAttributedString.dataFromRange , rtfd used a bit of different encoding, but I got what I need :) Also useful for some json conversion problems, etc.

Good luck :)

+3
Jul 06 '16 at 2:00
source share

Mark this answer if you need your data bytes as a string

stack overflow

+1
Oct 29 '14 at 7:54 on
source share



All Articles