宽字符 wchar_t

宽字符

宽字符(Wide character) 是电脑抽象术语(没有规定具体实现细节),表示比8位字符还宽的数据类型。不同于Unicode。

Unicode

ISO/IEC 10646:2003 Unicode4.0 指出:

"The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers." 翻译:“wchar_t的宽度属于编译器的特性,且可以小到8位。所以程序若需要跨过所有C和C++ 编译器的可携性,就不应使用wchar_t存储Unicode文字。wchar_t类型是为存储编译器定义的宽字符,在部分编译器中,其可以是Unicode字符。”

"ANSI/ISO C leaves the semantics of the wide character set to the specific implementation but requires that the characters from the portable C execution set correspond to their wide character equivalents by zero extension."

操作系统

对于Windows API及Visual Studio编译器,wchar_t是16位宽。由于不能在单个wchar_t字符中,支持系统所有可表示的字符(即UTF-16小尾字符),因而破坏了ANSI/ISO C标准。

在类Unix系统中,wchar_t是32位宽。单个wchar_t字符可表示任意UTF-32大尾字符。

程序设计语言

C/C++

wchar_t在ANSI/ISO C中是一个数据类型。某些其它的编程语言也用它来表示宽字符。在   ANSI C程序库头文件中,<wchar.h>和<wctype.h>处理宽字符。

最初,C90语言标准定义了类型wchar_t

"an integral type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales" (ISO 9899:1990 §4.1.5)

C语言与C++语言于2011年发布的各自语言标准中引入了固定大小的字符类型char16_tchar32_twchar_t仍保持由编译器实现定义其细节。

Python

Python使用wchar_t作为字符类型Py_UNICODE的基础。它依赖于该系统是否 wchar_t“兼容于被选择的Python Unicode编译版本”。

宽窄转换

任何非宽字符的字符集,无论是单字节字符集(SBCS),还是(可变长)多字节字符集(MBCS),都称作窄字符集(narrow character set)。

宽字符集的一个用途,是作为任意两个窄字符集相互转换的中间表示。

宽字符集与窄字符集的转换,有多种方法。

使用Windows AP

例如:

// we want to convert an MBCS string in lpszA  
int nLen = MultiByteToWideChar(CP_ACP,
    0,
    lpszA, -1,
    NULL,
    NULL);

LPWSTR lpszW = new WCHAR[nLen];  
MultiByteToWideChar(CP_ACP,
    0,   
    lpszA, -1,
    lpszW,
    nLen);

// use it to call OLE here  
pI->SomeFunctionThatNeedsUnicode(lpszW);

// free the string  
delete[] lpszW;

C语言标准库函数mbstowcs()和wcstombs()

定义于stdlib.h。需要预先分配目标缓冲区。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值