问题描述
我正在使用stringWithFormat @%ls来执行此操作,我只看到复制的第一个字符,这让我觉得它仍然假设它是一个单字节字符。
I'm using stringWithFormat @"%ls" to do it and I only see the first character copied, which makes me think it's still assuming it's a single byte char.
任何想法?
推荐答案
使用 initWithBytes:length:encoding
。您必须知道 wchar_t
使用的编码,我相信它在Apple平台上是UTF-32。
Use initWithBytes:length:encoding
. You will have to know the encoding that wchar_t
uses, I believe it is UTF-32 on Apple platforms.
#if defined(__BIG_ENDIAN__)
# define WCHAR_ENCODING NSUTF32BigEndianStringEncoding
#elif defined(__LITTLE_ENDIAN__)
# define WCHAR_ENCODING NSUTF32LittleEndianStringEncoding
#endif
[[NSString alloc] initWithBytes:mystring
length:(mylength * 4) encoding:WCHAR_ENCODING]
一般情况下,我建议尽量避免使用 wchar_t
因为它不是非常便携。特别是,你应该如何找出它使用的编码? (在Windows上它是UTF-16LE或UTF-16BE,在OS X,Linux和iOS上它是UTF-32LE或UTF-32BE)。
In general, I suggest avoid using wchar_t
if at all possible because it is not very portable. In particular, how are you supposed to figure out what encoding it uses? (On Windows it's UTF-16LE or UTF-16BE, on OS X, Linux, and iOS it's UTF-32LE or UTF-32BE).
这篇关于如何将wchar_t复制到NSString中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!