使用BOM创建一个UTF-8字符串 [英] create an UTF-8 string with BOM

查看:118
本文介绍了使用BOM创建一个UTF-8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MD5函数和Base64编码生成用户密钥(用于登录所使用的API的数据层)

I'm using MD5 function and Base64 Encoding to generate a User Secret (used to login to data layer of the used API)

我在< a href =http://www.adwiza.com/t/calculate.htm =nofollow noreferrer> javascript 和它的罚款,但在Objective C我困扰BOM

I did the code in javascript and it's fine, but in Objective C I'm strugling with the BOM

我的代码是:

NSString *str = [[NSString alloc] 
                 initWithFormat:@"%@%@%@%d", 
                    [auth uppercaseString], 
                    [user uppercaseString], 
                    [pwd uppercaseString], 
                    totalDaysSince2000];

NSString *sourceString = [[NSString alloc] initWithFormat:@"%02x%02x%02x%@", 
                          0xEF, 
                          0xBB, 
                          0xBF, 
                          str]; 

NSString *strMd5 = [sourceString MD5]; 

NSData *sourceData = [strMd5 dataUsingEncoding:NSUTF8StringEncoding];  
NSString *base64EncodedString = [[sourceData base64EncodedString] autorelease];  

使用上面的代码进入内存:

using the code above I'm getting into the memory:

女巫不是我真正需要的...

witch is not what I really need...

我甚至尝试过

"%c%c%c%@", (char)239, (char)187, (char)191, str

没有运气...

使用 UTF8String seam

如何正确添加BOM?

推荐答案

尝试直接在格式字符串中嵌入BOM作为转义字符文字:

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

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

这篇关于使用BOM创建一个UTF-8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆