create an UTF-8 string with BOM

和自甴很熟 提交于 2019-11-29 10:46:21

Try embedding the BOM directly in the format string as escaped character literals:

NSString *sourceString = [[NSString alloc] initWithFormat:@"\357\273\277%@", str];

You might have to add the BOM to the NSData object, not the NSString. Something like this:

char BOM[] = {0xEF, 0xBB, 0xBF};
NSMutableData* data = [NSMutableData data];
[data appendBytes:BOM length:3];
[data appendData:[strMd5 dataUsingEncoding:NSUTF8StringEncoding]];

I had similar issue with Swift and opening CSV fime in Excel. This question also helped me a lot.

Simple solution for swift with CSV file:

let BOM = "\u{FEFF}"
csvFile.append(BOM)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!