锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / ios开源技术 / 怎样把wchar_t转换为NSString
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究


怎样把wchar_t转换为NSString

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时,有时它得到正确的结果,但有时不是。

我错过了什么?

答1

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:

  • numberOfCharsInBuffer is the number of wchar_ts in the buffer that you want to decode. The method above does not assume that the string is null terminated and will happily try to put nulls into the NSString if they appear before the length you specify (note that with wchar_t "null" means a 32 bit value that is zero).
  • someEncoding is the encoding used by the string in the buffer. Try NSUTF32StringEncoding, NSUTF32LittleEndianStringEncoding, NSUTF32BigEndianStringEncoding.
  • numberOfCharsInBuffer是wchar_t要解码的缓冲区中的s 数。上面的方法并不假设字符串是空终止的,并且如果它们出现在您指定的长度之前,将很乐意尝试将空值放入NSString中(注意,使用wchar_t“null”表示32位值为零)。
  • someEncoding是缓冲区中字符串使用的编码。尝试NSUTF32StringEncoding,NSUTF32LittleEndianStringEncoding,NSUTF32BigEndianStringEncoding

答2

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]; }

答3

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

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内