开发者

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

开发者 https://www.devze.com 2023-01-11 09:01 出处:网络
When I try to convert a NSString object into a NSdata object, I use the following code: NSString *finalWordList = wordList.text;

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 m开发者_JS百科y 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.


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.


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.


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).


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消