新闻资讯
看你所看,想你所想

WideCharToMultiByte

WideCharToMultiByte

WideCharToMultiByte

WideCharToMultiByte是一个函式,该函式可以映射一个unicode字元串到一个多位元组字元串,执行转换的代码页、接收转换字元串、允许额外的控制等操作。

基本介绍

  • 中文名:宽字元到多位元组
  • 外文名:WideCharToMultiByte
  • 类型:函式
  • 作用:执行转换的代码页

基本介绍及功能

WideCharToMultiByte
函式原型:
int WideCharToMultiByte(
UINT CodePage, //指定执行转换的代码页
DWORD dwFlags, //允许你进行额外的控制,它会影响使用了读音符号(比如重音)的字元
LPCWSTR lpWideCharStr, //指定要转换为宽位元组字元串的缓冲区
int cchWideChar, //指定由参数lpWideCharStr指向的缓冲区的字元个数
LPSTR lpMultiByteStr, //指向接收被转换字元串的缓冲区
int cchMultiByte, //指定由参数lpMultiByteStr指向的缓冲区最大值
LPCSTR lpDefaultChar, //遇到一个不能转换的宽字元,函式便会使用pDefaultChar参数指向的字元
LPBOOL pfUsedDefaultChar //至少有一个字元不能转换为其多位元组形式,函式就会把这个变数设为TRUE
);
参数:
CodePage:指定执行转换的代码页,这个参数可以为系统已安装或有效的任何代码页所给定的值。你也可以指定其为下面的任意一值:
CP_ACP:ANSI代码页;CP_MACCP:Macintosh代码页;CP_OEMCP:OEM代码页;
CP_SYMBOL:符号代码页(42);CP_THREAD_ACP:当前执行绪ANSI代码页;
CP_UTF7:使用UTF-7转换;CP_UTF8:使用UTF-8转换。
dwFlags[in] Specifies the handling of unmapped characters. The function performs more quickly when none of these flags is set. The following flag constants are defined.
Value
Meaning
WC_NO_BEST_FIT_CHARS
Windows 98/Me and Windows 2000/XP: Any Unicode characters that do not translate directly to multibyte equivalents are translated to the default character (see lpDefaultCharparameter). In other words, if translating from Unicode to multibyte and back to Unicode again does not yield the exact same Unicode character, the default character is used. This flag can be used by itself or in combination with the other dwFlagoptions.
WC_COMPOSITECHECK
Convert composite characters to precomposed characters.
WC_DISCARDNS
Discard nonspacing characters during conversion.
WC_SEPCHARS
Generate separate characters during conversion. This is the default conversion behavior.
WC_DEFAULTCHAR
Replace exceptions with the default character during conversion.
When WC_COMPOSITECHECK is specified, the function converts composite characters to precomposed characters. A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/nonspacing character combination. In the character , the e is the base character, and the accent grave mark is the nonspacing character.
When an application specifies WC_COMPOSITECHECK, it can use the last three flags in this list (WC_DISCARDNS, WC_SEPCHARS, and WC_DEFAULTCHAR) to customize the conversion to precomposed characters. These flags determine the function's behavior when there is no precomposed mapping for a base/nonspace character combination in a wide-character string. These last three flags can only be used if the WC_COMPOSITECHECK flag is set.
The function's default behavior is to generate separate characters (WC_SEPCHARS) for unmapped composite characters.
For the code pages in the following table, dwFlagsmust be zero, otherwise the function fails with ERROR_INVALID_FLAGS.
50220 50221
50222
50225
50227 50229
52936
54936
57002 through 57011 65000 (UTF7)
65001 (UTF8)
42 (Symbol)

相关变数

lpWideCharStr:指向将被转换的unicode字元串。
cchWideChar:指定由参数lpWideCharStr指向的缓冲区的字元个数。如果这个值为-1,字元串将被设定为以NULL为结束符的字元串,并且自动计算长度。
lpMultiByteStr:指向接收被转换字元串的缓冲区。
cchMultiByte:指定由参数lpMultiByteStr指向的缓冲区最大值(用位元组来计量)。若此值为零,函式返回lpMultiByteStr指向的目标缓冲区所必需的位元组数,在这种情况下,lpMultiByteStr参数通常为NULL。
lpDefaultCharpfUsedDefaultChar:只有当WideCharToMultiByte函式遇到一个宽位元组字元,而该字元在uCodePage参数标识的代码页中并没有它的表示法时,WideCharToMultiByte函式才使用这两个参数。如果宽位元组字元不能被转换,该函式便使用lpDefaultChar参数指向的字元。如果该参数是NULL(这是大多数情况下的参数值),那幺该函式使用系统的默认字元。该默认字元通常是个问号。这对于档案名称来说是危险的,因为问号是个通配符。pfUsedDefaultChar参数指向一个布尔变数,如果Unicode字元串中至少有一个字元不能转换成等价多位元组字元,那幺函式就将该变数置为TRUE。如果所有字元均被成功地转换,那幺该函式就将该变数置为FALSE。当函式返回以便检查宽位元组字元串是否被成功地转换后,可以测试该变数。
返回值:如果函式运行成功,并且cchMultiByte不为零,返回值是由 lpMultiByteStr指向的缓冲区中写入的位元组数;如果函式运行成功,并且cchMultiByte为零,返回值是接收到待转换字元串的缓冲区所必需的位元组数。如果函式运行失败,返回值为零。若想获得更多错误信息,请调用GetLastError函式。它可以返回下面所列错误代码:
ERROR_INSUFFICIENT_BJFFER;ERROR_INVALID_FLAGS;
ERROR_INVALID_PARAMETER;ERROR_NO_UNICODE_TRANSLATION。
注意:指针lpMultiByteStr和lpWideCharStr必须不一样。如果一样,函式将失败,GetLastError将返回ERROR_INVALID_PARAMETER的值。
Windows CE:不支持参数CodePage中的CP_UTF7和CP_UTF8的值,以及参数dwFlags中的WC_NO_BEST_FIT_CHARS值。
速查:Windows NT 3.1、Windows 95以上、Windows CE 1.0以上,头档案:winnls.h;库档案:kernel32.lib。

转载请注明出处海之美文 » WideCharToMultiByte

相关推荐

    声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:ailianmeng11@163.com