xeron
xeron

Reputation: 255

Why is there is missing "\n" character when I convert NSString to NSdata?

When I try to convert a NSString object into a NSdata object, I use the following code:

NSString *finalWordList = wordList.text;
NSData *data = [finalWordList dataUsingEncoding:NSUTF8StringEncoding];

wordList is a UITextView object.

After that, I upload the NSdata object to my web server, which will be in .txt format. When I check the file, I notice that the \n character is missing in the uploaded txt file. Does anyone know why?

Any kind of help would be appreciated.

Upvotes: 0

Views: 1756

Answers (5)

dan
dan

Reputation: 31

I found out that replacing all "\n" characters with "\r\n" produces the right result, and the server can see the new line.

Upvotes: 0

xeron
xeron

Reputation: 255

I think I know the answer. It is because of the difference in operating system.

For iphone OS, \n means a new line. But for the display in windows notepad, a "\r" is also needed, i.e. "\r\n", such that it can display the new line character.

Upvotes: 0

JeremyP
JeremyP

Reputation: 86691

I assume you are expecting your string to be a line feed separated list of word.

Don't forget that for HTTP, the correct line ending is carriage return, line feed (%0D %0A).

Upvotes: 0

xeron
xeron

Reputation: 255

This is part of my code... I didn't post it here because it is a bit long, lol. What I was trying to do is that, I try to grab user input (from a UITextField) and display it in the screen (by a UITextView). After that, I grab the .text value from the UITextView, convert it from NSString to NSData, then upload the NSData object to a HTTP server, with a php upload script.

NSString *completeName = [NSString stringWithFormat:@"%@/%@.txt",mainDelegate.gCourseCode,mainDelegate.gUID];

NSString *finalWordList = wordList.text;
NSData *data = [finalWordList dataUsingEncoding:NSUTF8StringEncoding];

NSString *urlString = @"http://cetl.no-ip.org:50080/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",completeName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);

The txt file can be uploaded to the server. But it misses the \n character.

Thank you very much for your help.

Upvotes: 0

Akusete
Akusete

Reputation: 10794

Try this then come back with your updated results

NSString *finalWordList = wordList.text;
NSLog("finalWordList = [%@] %d", finalWordList, [finalWordList length]);
NSData *data = [finalWordList dataUsingEncoding:NSUTF8StringEncoding];
NSLog("data length = %d", [data length]);

I would bet that your NSData object is the correct length, meaning its your transfer/storage step which is introducing the error. Since your have not shown how you are doing that its rather difficult to say for sure.

Upvotes: 1

Related Questions