C char * 转换为 Objective-C NSString (或 NSData?)
我正在用 C 解析文件字节流,并通过回调将结果组织到 Objective-C 世界中的 NSDictionary 和 NSArray 中。
NSDictionary
的键都是 NSString
的实例。我使用 NSNEXTSTEPStringEncoding
将 C 字符串转换为 NSString
,但有时某些键是 nil
(即使 C字符串有一个或多个字符)。
这样做的正确方法是什么?
I'm parsing a file byte stream in C and organising the results into NSDictionary
s and NSArray
s in Objective-C world via callback.
The keys of an NSDictionary
are all instances of NSString
. I'm converting the C character strings into NSString
s with the NSNEXTSTEPStringEncoding
but now and again some of the keys are nil
(even when the C character strings have one or more characters).
What is the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSNEXTSTEPStringEncoding
是一种极其古老且罕见的字符串编码,可能是错误的使用方式。如果您的文本文件源自西欧或北美,则它们通常采用 Latin-1 或 UTF-8。现在我们假设使用 UTF-8(即NSUTF8StringEncoding
)。理想情况下,您会知道写入文件时使用的编码,并且在读取文件时会使用该编码。您的其余代码可能是正确的,因为您正在取回字符串。不是纯 7 位 ASCII 的字符串可能会给您带来麻烦。
NSNEXTSTEPStringEncoding
is an extremely old and rare string encoding and is probably the wrong one to use. Your text files will generally be in either Latin-1 or UTF-8, if they originated in Western Europe or in North America. Let's assume UTF-8 for now (that'sNSUTF8StringEncoding
.) Ideally, you'd know the encoding used when writing the files, and you'd use that when reading them.You presumably have the rest of the code correct, since you are getting strings back. It's the strings that aren't pure 7-bit ASCII that are likely giving you trouble.