“NSString stringWithUTF8String:”是过度触摸 [英] "NSString stringWithUTF8String:" is overly touchy
问题描述
我正在使用高级Cocoa功能,如 NSString
和 NSData
而不是挖掘C级的东西,比如工作在 char
的数组。
I'm in the middle of doing some string manipulation using high-level Cocoa features like NSString
and NSData
as opposed to digging down to C-level things like working on arrays of char
s.
它, + [NSString stringWithUTF8String:]
有时会在一个完美的字符串中返回 nil
c> - [NSString UTF8String] 。可以假定当输入格式错误时会发生这种情况。下面是十六进制输入失败的示例:
For the love of it, +[NSString stringWithUTF8String:]
sometimes returns nil
on a perfectly good string that was created with -[NSString UTF8String]
in the first place. One would assume that this happens when the input is malformed. Here is an example of the input that fails, in hex:
55 6B 66 51 35 59 4A 5C 6A 60 40 33 5F 45 58 60 9D 47 3F 6E 5E
60 59 34 58 68 41 4B 61 4E 3F 41 46 00
和ASCII:
UkfQ5YJ\j`@3_EX`G?n^`Y4XhAKaN?AF
这是一个随机生成的字符串,用于测试我的子程序。
This is a randomly generated string, to test my subroutine.
char * buffer = [randomNSString UTF8String];
// .... doing things .... in the end, buffer is the same as before
NSString * result = [NSString stringWithUTF8String:buffer];
// yields nil
编辑:为了防止有人没有理解隐式问题,这里是-v模式:
Just in case somebody didn't grasp the implicit question, here it is in -v mode:
为什么[NSString stringWithUTF8String:]有时会返回 nil
对完美形成的UTF8-String?
Why does [NSString stringWithUTF8String:] sometimes return nil
on a perfectly formed UTF8-String?
推荐答案
walkytalky是对的。 9d在utf8中不合法。具有顶部位10的utf8字节被保留为连续字符,它们不会出现没有前缀字符的多个前导位。
walkytalky is right. 9d is not legal in utf8 in this way. utf8 bytes with the top bits 10 are reserved as continuation characters, they never appear without a prefix character with more than one leading bit.
这篇关于“NSString stringWithUTF8String:”是过度触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!