开发者

doc file created in iPhone documents encoding issue

开发者 https://www.devze.com 2023-01-03 07:00 出处:网络
I\'m trying to write a MSword file in document directory by the following code: NSArray* paths开发者_JAVA技巧 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);

I'm trying to write a MSword file in document directory by the following code:

NSArray* paths开发者_JAVA技巧 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* path;
NSString* gradeDoc = [self fetchCommentsDesc];
NSString* str = [self.objStudent.strName stringByAppendingFormat:@".doc"];
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:str];
[gradeDoc writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

[self fetchCommentsDesc] returns NSString.
self.student.strName is a String

Issue: When i Open the doc file created in document directory of iphone, all the special characters in the doc appears as boxes or some arabic chars.

Please help!


You're writing some UTF-8 bytes, which is not a ".doc". Try writing it to a ".txt" file.

Also, you may have to write a "byte order mark" for some text editors to notice that it's UTF-8 or UTF-16 (in NS/CF-land, this is known as an "external representation"):

NSData * d = [(id)CFStringCreateExternalRepresentation(NULL, (CFStringRef)gradeDoc, kCFStringEncodingUTF8, 0) autorelease];
[d writeToFile:path atomically:YES];

Also try kCFStringEncodingUnicode and kCFStringEncodingUTF16.

0

精彩评论

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

关注公众号