将UTF-8编码的NSData转换为NSString我有UTF-8编码NSData在windows服务器上,我想将其转换为NSString为了iPhone。由于数据包含的字符(如程度符号)在两个平台上都有不同的值,如何将数据转换为字符串?
3 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
-initWithData:encoding:
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
-stringWithUTF8String:
\0
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
nil
.)
SWIFT变式:
let newStr = String(data: data, encoding: .utf8)// note that `newStr` is a `String?`, not a `String`.
// safe way, provided data is \0-terminatedlet newStr1 = String(data: data.subdata(in: 0 ..< data.count - 1), encoding: .utf8)// unsafe way, provided data is \0-terminatedlet newStr2 = data.withUnsafeBytes(String.init(utf8String:))
- 3 回答
- 0 关注
- 773 浏览
添加回答
举报
0/150
提交
取消