首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

关于MultiByteToWideChar的与WideCharToMultiByte的第一个参数,该如何处理

2012-01-15 
关于MultiByteToWideChar的与WideCharToMultiByte的第一个参数这个参数一的作用是什么?有人说是要转换的目

关于MultiByteToWideChar的与WideCharToMultiByte的第一个参数
这个参数一的作用是什么?有人说是要转换的目标codepage
但我试过后不是这样的.而msdn里说得也不清不楚的.
它只说be   used   to   perform   the   conversion,没说是源codepage还是目标codepage

例如,如果我的工程用多字符集
char   *pstr= "一二三 ";
int   len=MultiByteToWideChar(CP_UTF8,0,str,-1,0,0);//请求WCHAR字符数
len等于2(这里是错误的,正确应该是4,如果CP_UTF8改CP_ACP就正常,说明不是目标codepage)

那么就试试会不会是源codpage呢,结果:
WCHAR   *pwstr=L "一二三 ";
int   len=WideCharToMultiByte(CP_UTF8,0,pwstr,-1,0,0,NULL,NULL);//请求char字节数
len等于10(说明是错误的,正确应该是7)

然后我想是否和工程的字符集有关,我改为unicode字符集
结果还是发生同样的错误.

最后的结果是不知到它有什么用.只要用CP_ACP就OK了.

PS:如不会是于系统编码有关呢??




[解决办法]
根一个Word(字)的字节的高低字节的排列顺序有关,不同的操作系统和CPU有区别的。
[解决办法]
MultiByteToWideChar() maps a character string to a wide-character string. The declaration of this application programming interface (API) is as follows:


int MultiByteToWideChar(uCodePage, dwFlags, lpMultiByteStr,
cchMultiByte, lpWideCharStr, cchWideChar)

UINT uCodePage; /* codepage */
DWORD dwFlags; /* character-type options */
LPCSTR lpMultiByteStr; /* address of string to map */
int cchMultiByte; /* number of characters in string */
LPWSTR lpWideCharStr; /* address of wide-character buffer */
int cchWideChar; /* size of wide-character buffer */
The first parameter, uCodePage, specifies the codepage to be used when performing the conversion. This discussion applies to the first parameter of WideCharToMultiByte() as well. The codepage can be any valid codepage number. It is a good idea to check this number with IsValidCodepage(), even though MultiByteToWideChar() returns an error if an invalid codepage is used. The codepage may also be one of the following values:

CP_ACP ANSI codepage
CP_OEMCP OEM (original equipment manufacturer) codepage
CP_ACP instructs the API to use the currently set default Windows ANSI codepage. CP_OEMCP instructs the API to use the currently set default OEM codepage.

If Win32 ANSI APIs are used to get filenames from a Windows NT system, use CP_ACP when converting the string. Windows NT retrieves the name from the physical device and translates the OEM name into Unicode. The Unicode name is translated into ANSI if an ANSI API is called, then it can be translated back into Unicode with MultiByteToWideChar().

If filenames are being retrieved from a file that is OEM encoded, use CP_OEMCP instead.


MORE INFORMATION
When an application calls an ANSI function, the FAT/HPFS file systems will call AnsiToOem(); however, if an ANSI character does not exist in an OEM codepage, the filename will not be representable. In these cases, SetFileApisToOEM() should be called to prevent this problem by setting a group of the Win32 APIs to use the OEM codepage instead of the ANSI codepage.

[解决办法]
查看MSDN中的代码发现几乎只是使用CP_ACP
[解决办法]
WideCharToMultiByte(CP_UTF8,0,pwstr,-1,0,0,NULL,NULL);
CP_UTF8只能从宽字符和单字符之间转换,且UTF8是汉字有三个字符组成的
[解决办法]
The first parameter, uCodePage, specifies the codepage to be used when performing the conversion.

这个参数是当你执行转换的时候的操作系统当前码

比如GB是936,而BIG5是950

热点排行