谁能给个MultiByteToWideChar和WideCharToMultiBytcookie用法例子的例子

79128人阅读
WinCE(192)
//========================================================================//TITLE://&&& MultiByteToWideChar和WideCharToMultiByte用法详解//AUTHOR://&&& norains//DATE://&&& 第一版:Monday& 25-December -2006//&&& 增补版:Wednesday 27-December -2006//&&& 修订版:Wednesday 14-March-2007 (修正之前的错误例子)//&&& 再次修订版:Tuesday 18-September-2007 (修正代码的参数错误)
//&&& 再再次修订版:Friday 02-April-2009 (修正文章举例的错误)//Environment://& EVC4.0 + Standard SDK//========================================================================& 1.使用方法详解& 在本文开始之处,先简要地说一下何为短字符和宽字符.& 所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码.而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.关于windows下的ASCII和UNICODE的更多信息,可以参考这两本经典著作:《windows 程序设计》,《windows 核心编程》.这两本书关于这两种字符都有比较详细的解说.& & 宽字符转换为多个短字符是一个难点,不过我们只要掌握到其中的要领,便可如鱼得水.& 好吧,那就让我们开始吧.& & 这个是我们需要转化的多字节字符串: && char sText[20] = {"多字节字符串!OK!"};& & 我们需要知道转化后的宽字符需要多少个数组空间.虽然在这个里程里面,我们可以直接定义一个20个宽字符的数组(如果全部是中文,则宽字符数组需要10个;反之,如果是英文字符,则需要20个。这里选取的是最大值),并且事实上将运行得非常轻松愉快.但假如多字节字符串更多,达到上千个乃至上万个,我们将会发现其中浪费的内存将会越来越多.所以以多字节字符的个数的两倍作为宽字符数组下标的声明绝对不是一个好主意.& 所幸,我们能够确知所需要的数组空间.& 我们只需要将MultiByteToWideChar()的第四个形参设为-1,即可返回所需的宽字符数组空间的个数:& DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText, -1, NULL, 0);& & 接下来,我们只需要分配响应的数组空间:& wchar_t *pwT& pwText = new wchar_t[dwNum];& if(!pwText)& {&& delete []pwT& }& & 接着,我们就可以着手进行转换了.在这里以转换成ASCII码做为例子:& MultiByteToWideChar (CP_ACP, 0, sText, -1, pwText, dwNum);& & 最后,使用完毕当然要记得释放占用的内存:& delete []pwT& && 同理,宽字符转为多字节字符的代码如下: && wchar_t wText[20] = {L"宽字符转换实例!OK!"};& DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);& char *psT& psText = new char[dwNum];& if(!psText)& {&& delete []psT& }& WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);& delete []psT& && 如果之前我们已经分配好空间,并且由于字符串较短,可以不理会浪费的空间,仅仅只是想简单地将短字符和宽字符相互转换,那有没有什么简便的方法呢?&& WIN32 API里没有符合这种要求的函数,但我们可以自己进行封装:&&& && //-------------------------------------------------------------------------------------& //Description:& // This function maps a character string to a wide-character (Unicode) string& //& //Parameters:& // lpcszStr: [in] Pointer to the character string to be converted & // lpwszStr: [out] Pointer to a buffer that receives the translated string. & // dwSize: [in] Size of the buffer& //& //Return Values:& // TRUE: Succeed& // FALSE: Failed& // & //Example:& // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));& //---------------------------------------------------------------------------------------& BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)& {&&& // Get the required size of the buffer that receives the Unicode &&& // string. &&& DWORD dwMinS&&& dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);& &&& if(dwSize & dwMinSize)&&& {&&&& return FALSE;&&& }& && &&&& // Convert headers from ASCII to Unicode.&&& MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); &&&& return TRUE;& }& & //-------------------------------------------------------------------------------------& //Description:& // This function maps a wide-character string to a new character string& //& //Parameters:& // lpcwszStr: [in] Pointer to the character string to be converted & // lpszStr: [out] Pointer to a buffer that receives the translated string. & // dwSize: [in] Size of the buffer& //& //Return Values:& // TRUE: Succeed& // FALSE: Failed& // & //Example:& // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));& //---------------------------------------------------------------------------------------& BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)& {&& DWORD dwMinS&& dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);&& if(dwSize & dwMinSize)&& {&&& return FALSE;&& }&& WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);&& return TRUE;& }& & & 使用方法也很简单,示例如下:& wchar_t wText[10] = {L"函数示例"};& char sText[20]= {0};& WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));& MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));& & 这两个函数的缺点在于无法动态分配内存,在转换很长的字符串时可能会浪费较多内存空间;优点是,在不考虑浪费空间的情况下转换较短字符串非常方便.& 2.MultiByteToWideChar()函数乱码的问题& 有的朋友可能已经发现,在标准的WinCE4.2或WinCE5.0 SDK模拟器下,这个函数都无法正常工作,其转换之后的字符全是乱码.及时更改MultiByteToWideChar()参数也依然如此.& 不过这个不是代码问题,其结症在于所定制的操作系统.如果我们定制的操作系统默认语言不是中文,也会出现这种情况.由于标准的SDK默认语言为英文,所以肯定会出现这个问题.而这个问题的解决,不能在简单地更改控制面板的"区域选项"的"默认语言",而是要在系统定制的时候,选择默认语言为"中文".& 系统定制时选择默认语言的位置于:& Platform -& Setting... -& locale -& default language ,选择"中文",然后编译即可.
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:5373463次
积分:58721
积分:58721
排名:第34名
原创:496篇
转载:15篇
评论:13039条
文章:128篇
阅读:3113441
(1)(1)(1)(1)(5)(1)(9)(6)(9)(9)(12)(9)(9)(7)(9)(9)(9)(9)(13)(14)(10)(6)(12)(2)(7)(4)(7)(14)(13)(18)(3)(4)(8)(5)(12)(7)(6)(6)(6)(1)(4)(3)(8)(3)(2)(14)(4)(2)(2)(1)(3)(6)(3)(8)(8)(5)(4)(12)(11)(4)(5)(3)(4)(2)(2)(6)(10)(8)(2)(13)(4)(4)(4)(4)(6)(5)(4)(12)(3)(5)(2)(2)MultiByteToWideChar function (Windows)
MultiByteToWideChar
Expand the table of content
MultiByteToWideChar function
Maps a character string to a UTF-16 (wide character) string. The character string is not necessarily from a multibyte character set.Caution
Using the MultiByteToWideChar function incorrectly can compromise the security of your application. Calling this function can easily cause a buffer overrun because the size of the input buffer indicated by lpMultiByteStr equals the number of bytes in the string, while the size of the output buffer indicated by lpWideCharStr equals the number of characters. To avoid a buffer overrun, your application must specify a buffer size appropriate for the data type the buffer receives. For more information, see .
The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 or UTF-16, instead of a specific code page, unless legacy standards or data formats prevent the use of Unicode. If using Unicode is not possible, applications should tag the data stream with the appropriate encoding name when protocols allow it. HTML and XML files allow tagging, but text files do not.
int MultiByteToWideChar(
LPCSTR lpMultiByteStr,
cbMultiByte,
_Out_opt_ LPWSTR lpWideCharStr,
cchWideChar
Parameters
CodePage [in]
Code page to use in performing the conversion. This parameter can be set to the value of any code page that is installed or available in the operating system. For a list of code pages, see . Your application can also specify one of the values shown in the following table.
ValueMeaning
The system default Windows ANSI code page.
This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.
The current system Macintosh code page.
This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.
This value is used primarily in legacy code and should not generally be needed since modern Macintosh computers use Unicode for encoding.
The current system OEM code page.
This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.
Symbol code page (42).
CP_THREAD_ACP
The Windows ANSI code page for the current thread.
This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.
UTF-7. Use this value only when forced by a 7-bit transport mechanism. Use of UTF-8 is preferred.
dwFlags [in]
Flags indicating the conversion type. The application can specify a combination of the following values, with MB_PRECOMPOSED being the default. MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. MB_USEGLYPHCHARS and MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags.
ValueMeaning
MB_COMPOSITE
Always use decomposed characters, that is, characters in which a base character and one or more nonspacing characters each have distinct code point values. For example, ? is represented by A + ¨: LATIN CAPITAL LETTER A (U+0041) + COMBINING DIAERESIS (U+0308). Note that this flag cannot be used with MB_PRECOMPOSED.
MB_ERR_INVALID_CHARS
Fail if an invalid input character is encountered.
Starting with Windows Vista, the function does not drop illegal code points if the application does not set this flag.
Windows 2000 with SP4 and later, Windows XP:
If this flag is not set, the function silently drops illegal code points. A call to
returns ERROR_NO_UNICODE_TRANSLATION.
MB_PRECOMPOSED
D do not use with MB_COMPOSITE. Always use precomposed characters, that is, characters having a single character value for a base or nonspacing character combination. For example, in the character è, the e is the base character and the accent grave mark is the nonspacing character. If a single Unicode code point is defined for a character, the application should use it instead of a separate base character and a nonspacing character. For example, ? is represented by the single Unicode code point LATIN CAPITAL LETTER A WITH DIAERESIS (U+00C4).
MB_USEGLYPHCHARS
Use glyph characters instead of control characters.
For the code pages listed below, dwFlags must be set to 0. Otherwise, the function fails with ERROR_INVALID_FLAGS.
57002 through 57011
65000 (UTF-7)
42 (Symbol)
For UTF-8 or code page 54936 (GB18030, starting with Windows Vista), dwFlags must be set to either 0 or MB_ERR_INVALID_CHARS. Otherwise, the function fails with ERROR_INVALID_FLAGS.
lpMultiByteStr [in]
Pointer to the character string to convert.
cbMultiByte [in]
Size, in bytes, of the string indicated by the lpMultiByteStr parameter. Alternatively, this parameter can be set to -1 if the string is null-terminated. Note that, if cbMultiByte is 0, the function fails.
If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting Unicode string has a terminating null character, and the length returned by the function includes this character.
If this parameter is set to a positive integer, the function processes exactly the specified number of bytes. If the provided size does not include a terminating null character, the resulting Unicode string is not null-terminated, and the returned length does not include this character.
lpWideCharStr [out, optional]
Pointer to a buffer that receives the converted string.
cchWideChar [in]
Size, in characters, of the buffer indicated by lpWideCharStr. If this value is 0, the function returns the required buffer size, in characters, including any terminating null character, and makes no use of the lpWideCharStr buffer.
Return value
Returns the number of characters written to the buffer indicated by lpWideCharStr if successful. If the function succeeds and cchWideChar is 0, the return value is the required size, in characters, for the buffer indicated by lpWideCharStr. If the input byte/char sequences are invalid, returns U+FFFD for UTF encodings.
The function returns 0 if it does not succeed. To get extended error information, the application can call , which can return one of the following error codes:
ERROR_INSUFFICIENT_BUFFER. A supplied buffer size was not large enough, or it was incorrectly set to NULL.
ERROR_INVALID_FLAGS. The values supplied for flags were not valid.
ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.
ERROR_NO_UNICODE_TRANSLATION. Invalid Unicode was found in a string.
The default behavior of this function is to translate to a precomposed form of the input character string. If a precomposed form does not exist, the function attempts to translate to a composite form.
The use of the MB_PRECOMPOSED flag has very little effect on most code pages because most input data is composed already. Consider calling
after converting with MultiByteToWideChar. NormalizeString provides more accurate, standard, and consistent data, and can also be faster. Note that for the
enumeration being passed to NormalizeString, NormalizationC corresponds to MB_PRECOMPOSED and NormalizationD corresponds to MB_COMPOSITE.
As mentioned in the caution above, the output buffer can easily be overrun if this function is not first called with cchWideChar set to 0 in order to obtain the required size. If the MB_COMPOSITE flag is used, the output can be three or more characters long for each input character.
The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and
returns the value ERROR_INVALID_PARAMETER.
MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character. To null-terminate an output string for this function, the application should pass in -1 or explicitly count the terminating null character for the input string.
The function fails if MB_ERR_INVALID_CHARS is set and an invalid character is encountered in the source string. An invalid character is one of the following:
A character that is not the default character in the source string, but translates to the default character when MB_ERR_INVALID_CHARS is not set
For DBCS strings, a character that has a lead byte but no valid trail byte
Starting with Windows Vista, this function fully conforms with the Unicode 4.1 specification for UTF-8 and UTF-16. The function used on earlier operating systems encodes or decodes lone
halves or mismatched surrogate pairs. Code written in earlier versions of Windows that rely on this behavior to encode random non-text binary data might run into problems. However, code that uses this function on valid UTF-8 strings will behave the same way as on earlier Windows operating systems.
Windows XP: To prevent the security problem of the non-shortest-form versions of UTF-8 characters, MultiByteToWideChar deletes these characters.
Starting with Windows 8: MultiByteToWideChar
is declared in Stringapiset.h. Before Windows 8, it was declared in Winnls.h.
Requirements
Minimum supported client
Windows 2000 Professional [desktop apps | Windows Store apps]
Minimum supported server
Windows 2000 Server [desktop apps | Windows Store apps]
Minimum supported phone
Windows Phone 8
Stringapiset.h (include Windows.h)
Kernel32.lib
Kernel32.dll
IN THIS ARTICLE
Is this page helpful?
Additional feedback?
1500 characters remaining
Thank you!
We appreciate your feedback.
(C) Microsoft 2017Linux下面的WideCharToMultiByte()和MultiByteToWideChar()-红联Linux系统门户
您的位置:
&& 查看内容 - - -
Linux下面的WideCharToMultiByte()和MultiByteToWideChar()
Dopsdck发布于
&&字号: &&&&(网友评论&0&条)&
Linux下面的没有命名为 WideCharToMultiByte() 和 MultiByteToWideChar() 函数,WideCharToMultiByte,MultiByteToWideChar是windows下的函数,在linux下也有类似的两个函数:
mbstowcs()
wcstombs()
值得注意的是:
size_t mbstowcs(
wchar_t *wcstr,
const char *mbstr,
size_t count
这个函数的第三个参数,大小一定要是mbstr长度的2倍,否则出来的中文也会是乱码。
发表评论,与各位同人交流。回复请点击下方的我要评论按钮(游客可回复),要发表贴子请点击
Linux教程下载?“”(请点击),Linux教程免费下载。
求助Linux问题?论坛有39版块,覆盖所有Linux技术层面。前往“”
 |  |  |  |  |  |  |  |  |  |  |  | 
&2017 红联 Powered by SupSiteMFC学习(127)
先看看这篇关于Windows编码的文章:
  再看看这篇关于两个函数参数和用法的说明:
  为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页。
  WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
  MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个:
  使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
  使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
1. ANSI to Unicode
1 wstring ANSIToUnicode( const string& str )
3 int len = 0;
4 len = str.length();
5 int unicodeLen = ::MultiByteToWideChar( CP_ACP,
str.c_str(),
11 wchar_t * pU
12 pUnicode = new wchar_t[unicodeLen+1];
13 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
14 ::MultiByteToWideChar( CP_ACP,
str.c_str(),
(LPWSTR)pUnicode,
unicodeLen );
21 rt = ( wchar_t* )pU
22 delete pU
2. Unicode to ANSI
1 string UnicodeToANSI( const wstring& str )
5 // wide char to multi char
6 iTextLen = WideCharToMultiByte( CP_ACP,
str.c_str(),
14 pElementText = new char[iTextLen + 1];
15 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
16 ::WideCharToMultiByte( CP_ACP,
str.c_str(),
pElementText,
24 string strT
25 strText = pElementT
26 delete[] pElementT
27 return strT
3. UTF-8 to Unicode
1 wstring UTF8ToUnicode( const string& str )
3 int len = 0;
4 len = str.length();
5 int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
str.c_str(),
11 wchar_t * pU
12 pUnicode = new wchar_t[unicodeLen+1];
13 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
14 ::MultiByteToWideChar( CP_UTF8,
str.c_str(),
(LPWSTR)pUnicode,
unicodeLen );
21 rt = ( wchar_t* )pU
22 delete pU
4. Unicode to UTF-8
1 string UnicodeToUTF8( const wstring& str )
5 // wide char to multi char
6 iTextLen = WideCharToMultiByte( CP_UTF8,
str.c_str(),
14 pElementText = new char[iTextLen + 1];
15 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
16 ::WideCharToMultiByte( CP_UTF8,
str.c_str(),
pElementText,
24 string strT
25 strText = pElementT
26 delete[] pElementT
27 return strT
示例下载地址:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:67117次
积分:1622
积分:1622
排名:千里之外
原创:66篇
转载:110篇
博客或则下载的DEMO有什么问题,都可以进群问我,我基本上大部分时间都会在线的. 群内还有不少大神在线回答问题! 群号:.欢迎新人,大神都来进驻! 点击链接加入群/?_wv=1027&k=2BDqevm
(4)(6)(2)(2)(3)(10)(5)(6)(18)(25)(21)(22)(15)(16)(6)(3)(5)(5)(网摘)MultiByteToWideChar跟WideCharToMultiByte用法详解
MultiByteToWideChar和WideCharToMultiByte用法详解 [url=] MultiByteToWideChar和WideCharToMultiByte用法详解[/url] //======================================================================== //TITLE: //&&&&MultiByteToWideChar和WideCharToMultiByte用法详解 //AUTHOR: //&&&&norains //DATE: //&&&&初版:Monday&&25-December -2006 //&&&&增补版:Wednesday 27-December -2006 //&&&&订正版:Wednesday 14-March-2007 (修改之前的过错例子) //Environment: //&&EVC4.0 + Standard SDK //======================================================================== && 1.使用方法详解 &&在本文开始之处,先扼要地说一下作甚短字符和宽字符. &&所谓的短字符,就是用8bit来表示的字符,典范的利用是ASCII码.而宽字符,顾名思义,就是用16bit表现的字符,典型的有UNICODE.对于windows下的ASCII和UNICODE的更多信息,可以参考这两本经典著述:《windows 程序设计》,《windows 中心编程》.这两本书关于这两种字符都有比拟具体的讲解. && &&宽字符转换为多个短字符是一个难点,不过我们只有控制到其中的要领,便可如鱼得水. &&好吧,那就让我们开端吧. && &&这个是我们需要转化的多字节字符串:&& &&char sText[20] = {&多字节字符串!OK!&}; && &&我们需要晓得转化后的宽字符需要多少个数组空间.固然在这个里程里面,我们可以直接定义一个20*2宽字符的数组,并且事实上将运行得十分轻松高兴.但如果多字节字符串更多,到达上千个乃至上万个,我们将会发明其中糟蹋的内存将会越来越多.所以以多字节字符的个数的两倍作为宽字符数组下标的申明相对不是一个好主张. &&所幸,咱们可能确知所须要的数组空间. &&我们只需要将MultiByteToWideChar()的第四个形参设为-1,即可返回所需的短字符数组空间的个数: &&DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText, -1, NULL, 0); && &&接下来,我们只要要分配响应的数组空间: &&wchar_t *pwT &&pwText = new wchar_t[dwNum]; &&if(!pwText) &&{ && delete []pwT &&} && &&接着,我们就能够着手进行转换了.在这里以转换成ASCII码做为例子: &&MultiByteToWideChar (CP_ACP, 0, psText, -1, sText, dwSize); && &&最后,使用结束当然要记得开释占用的内存: &&delete []psT && &&同理,宽字符转为多字节字符的代码如下:&& &&wchar_t wText[20] = {L&宽字符转换实例!OK!&}; &&DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE); &&char *psT &&psText = new char[dwNum]; &&if(!psText) &&{ && delete []psT &&} &&WideCharToMultiByte (CP_OEMCP,NULL,lpcwszStr,-1,psText,dwNum,NULL,FALSE); &&delete []psT && && 如果之前我们已经调配好空间,并且因为字符串较短,可以不理睬挥霍的空间,仅仅只是想简单地将短字符跟宽字符彼此转换,那有没有什么简便的方式呢? && WIN32 API里不合乎这种请求的函数,但我们可以本人进行封装: &&&& &&//------------------------------------------------------------------------------------- &&//Description: &&// This function maps a character string to a wide-character (Unicode) string &&// &&//Parameters: &&// lpcszStr: [in] Pointer to the character string to be converted &&// lpwszStr: [out] Pointer to a buffer that receives the translated string. &&// dwSize: [in] Size of the buffer &&// &&//Return Values: &&// TRUE: Succeed &&// FALSE: Failed &&// &&//Example: &&// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0])); &&//--------------------------------------------------------------------------------------- &&BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize) &&{ &&&&// Get the required size of the buffer that receives the Unicode &&&&// string. &&&&DWORD dwMinS &&&&dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0); && &&&&if(dwSize & dwMinSize) &&&&{ &&&& return FALSE; &&&&} && &&&& &&&&// Convert headers from ASCII to Unicode. &&&&MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);&& &&&&return TRUE; &&} && &&//------------------------------------------------------------------------------------- &&//Description: &&// This function maps a wide-character string to a new character string &&// &&//Parameters: &&// lpcwszStr: [in] Pointer to the character string to be converted &&// lpszStr: [out] Pointer to a buffer that receives the translated string. &&// dwSize: [in] Size of the buffer &&// &&//Return Values: &&// TRUE: Succeed &&// FALSE: Failed &&// &&//Example: &&// MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0])); &&//--------------------------------------------------------------------------------------- &&BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize) &&{ && DWORD dwMinS && dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE); && if(dwSize & dwMinSize) && { &&&&return FALSE; && } && WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE); && return TRUE; &&} && && &&应用办法也很简单,示例如下: &&wchar_t wText[10] = {L&函数示例&}; &&char sText[20]= {0}; &&WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0])); &&MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0])); && &&这两个函数的毛病在于无法动态分配内存,在转换很长的字符串时可能会浪费较多内存空间;长处是,在不斟酌浪费空间的情况下转换较短字符串无比便利. && 2.MultiByteToWideChar()函数乱码的问题 &&有的友人可能已经发现,在标准的WinCE4.2或WinCE5.0 SDK模仿器下,这个函数都无奈畸形工作,其转换之后的字符全是乱码.及时更改MultiByteToWideChar()参数也仍然如斯. &&不外这个不是代码问题,其结症在于所定制的操作系统.假如我们定制的操作系统默认语言不是中文,也会呈现这种情形.因为尺度的SDK默认语言为英文,所以确定会涌现这个问题.而这个问题的解决,不能在简略地更改把持面板的&区域选项&的&默认语言&,而是要在系统定制的时候,挑选默认语言为&中文&. &&体系定制时取舍默认语言的地位于: &&Platform -& Setting... -& locale -& default language ,抉择&中文&,而后编译即可. &&
请各位遵纪守法并注意语言文明

我要回帖

更多关于 xptable的用法例子 的文章

 

随机推荐