精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
I have wchar_t buffer [100] . Sometimes it needed for Unicode letters, sometimes is not.
I need to convert it to NSString.
I'm using NSString *str = [NSString string:(char *)buffer]; to conver it.
When I'm trying to NSLog my NSString, sometimes it getting right result, but sometimes is not.
Did I miss something?我有wchar_t buffer [100]。有时它需要Unicode字母,有时则不需要。
我需要将其转换为NSString。
我正在使用 NSString *str = [NSString string:(char *)buffer];它来转换它。
当我尝试NSLog我的NSString时,有时它得到正确的结果,但有时不是。
我错过了什么?
Everything is as Totumus Maximus has said, but additionally you need to know how the characters in your buffer are encoded. As wchar_t is 32 bits you probably have some 32 bit encoding of which UTF32-LE is the most likely. What you want to do to get your NSString is:一切都像Totumus Maximus所说,但另外你需要知道缓冲区中的字符是如何编码的。与wchar_t32位一样,您可能有一些32位编码,其中UTF32-LE最有可能。你想要获得NSString的方法是:
NSString* result = [[NSString alloc] initWithBytes: (const void*)buffer length: sizeof(wchar_t) * numberOfCharsInBuffer encoding: someEncoding];
where:
My converter for "char", "wchar_t", "NSString". Use and enjoy.
//-=(W)=- +(NSString *)stringFromChar:(const char *)charText { return [NSString stringWithUTF8String:charText]; } +(const char *)charFromString:(NSString *)string { return [string cStringUsingEncoding:NSUTF8StringEncoding]; } +(NSString *)stringFromWchar:(const wchar_t *)charText { //used ARC return [[NSString alloc] initWithBytes:charText length:wcslen(charText)*sizeof(*charText) encoding:NSUTF32LittleEndianStringEncoding]; } +(const char /*wchar_t*/ *)wcharFromString:(NSString *)string { return [string cStringUsingEncoding:NSUTF8StringEncoding]; }
I think the correct implementation of wcharFromString would be +(const wchar_t *)wcharFromString:(NSString *)string { return (const wchar_t *)[string cStringUsingEncoding:NSUTF32LittleEndianStringEncoding]; }我认为正确的实现wcharFromString将是+(const wchar_t *)wcharFromString:(NSString *)string { return (const wchar_t *)[string cStringUsingEncoding:NSUTF32LittleEndianStringEncoding]; }
Maybe this will clear things up.
C89 introduced a new integer type, wchar_t. This is similar to a char, but typically "wider". On many systems, including Windows, a wchar_t is 16 bits. This is typical of systems that implemented their Unicode support using earlier versions of the Unicode standard, which originally defined fewer than 65,535 characters. Unicode was later expanded to support historical and special purpose character sets, so on some systems, including Mac OS X and iOS, the wchar_t type is 32 bits in size. This is often poorly documented, but you can use a simple test like this to find out:也许这会让事情变得清晰起来。
C89引入了一个新的整数类型,wchar_t。这类似于char,但通常“更宽”。在许多系统上,包括Windows,a wchar_t是16位。这是使用早期版本的Unicode标准实现其Unicode支持的系统的典型,该标准最初定义的字符少于65,535个字符。后来扩展了Unicode以支持历史和专用字符集,因此在某些系统上,包括Mac OS X和iOS,wchar_t类型的大小为32位。这通常记录很少,但您可以使用这样的简单测试来找出:
// how big is wchar_t? NSLog(@"wchar_t is %u bits wide", 8 * sizeof(wchar_t));
On a Mac or iPhone, this will print "wchar_t is 32 bits wide". Additionally, wchar_t is a typedef for another integer type in C. In C++, wchar_t is a built-in integer type. In practice, this means you need to #include in C when using wide characters.在Mac或iPhone上,这将打印“wchar_t为32位宽”。另外,wchar_t是C中另一个整数类型的typedef。在C ++中,wchar_t是一个内置的整数类型。实际上,这意味着在使用宽字符时需要在C中使用#include。
Ref: http://blog.ablepear.com/2010/07/objective-c-tuesdays-wide-character.html