我用msdn的例子来试验: #include “stdafx.h” #include <stdio.h> #define BUFFER_SIZE 100 int main( void ) printf( “Convert wide-character string:\n” ); // Conversion // Output // Free multibyte character buffer msdn的代码肯定是没事的,但我把上面(1)句注释掉换成(2)句,即字串换成中文,就不行了!打印不出来转换后的文字,设断点查pMBBuffer,发现wcstombs_s调用后pMBBuffer是空的,什么都没转出来。 为什么英文可以中文不行?why? |
|
40分 |
调用wcstombs之前先调setlocale(LC_ALL, “zh-CN”);否则wcstombs不会处理超过255的字符,不过这到底是bug还是标准就不知道了。
|
原则上应该是setlocale(LC_CTYPE, “zh-CN”);,因为wcstombs用的不是系统code page而是设置LC_CTYPE时候获得的code page,LC_CTYPE默认是空的。
|
|
在wcstombs_s()前加了setlocale(LC_ALL, “zh-CN”)或setlocale(LC_CTYPE, “zh-CN”)甚至两句都加也还不行呀。还是转不出中文。
|
|
Convert wide-character string:
Characters converted: 12 Multibyte character: 世界, 你好. Press any key to continue . . . 那就不知道是怎么回事鸟… |
|
查了下,我这里要写:
setlocale(LC_ALL,””); 或者 setlocale(LC_ALL, “chs”); 难道不同机器或不同系统有关系? |
|
嗯… 貌似直接用locale name是2012才加的。
|
|
据说
setlocale(LC_ALL,”chs”); 和 _wsetlocale(LC_ALL,L”chs”); 不是一回事。 |
|
20分 |
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript
Visual Basic C# Visual C++ J# JScript Run-Time Library Reference wcstombs_s, _wcstombs_s_l Example See Also Send Feedback Converts a sequence of wide characters to a corresponding sequence of multibyte characters. A version of wcstombs, _wcstombs_l with security enhancements as described in Security Enhancements in the CRT. Parameters [out] mbstr [in]sizeInBytes [in] wcstr [in] count [in] locale Return Value Error condition If any of these conditions occurs, the invalid parameter exception is invoked as described in Parameter Validation . If execution is allowed to continue, the function returns an error code and sets errno as indicated in the table. Remarks A null wide character is encountered A wide character that cannot be converted is encountered The number of bytes stored in the mbstr buffer equals count. The destination string is always null-terminated (even in the case of an error). If count is the special value _TRUNCATE, then wcstombs_s converts as much of the string as will fit into the destination buffer, while still leaving room for a null terminator. If wcstombs_s successfully converts the source string, it puts the size in bytes of the converted string, including the null terminator, into *pReturnValue (provided pReturnValue is not NULL). This occurs even if the mbstr argument is NULL and provides a way to determine the required buffer size. Note that if mbstr is NULL, count is ignored. If wcstombs_s encounters a wide character it cannot convert to a multibyte character, it puts 0 in *pReturnValue, sets the destination buffer to an empty string, sets errno to EILSEQ, and returns EILSEQ. If the sequences pointed to by wcstr and mbstr overlap, the behavior of wcstombs_s is undefined. Security Note: Requirements For additional compatibility information, see Compatibility in the Introduction. Example Copy Code #define BUFFER_SIZE 100 int main( void ) printf( “Convert wide-character string:\n” ); // Conversion // Output // Free multibyte character buffer .NET Framework Equivalent See Also |
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript
Visual Basic C# Visual C++ J# JScript Run-Time Library Reference Locale See Also Send Feedback The “Locale” refers to the locality (the Country/Region, and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. For more information, see Locale Categories. Use the setlocale function to change or query some or all of the current program or thread locale information while using functions without the _l suffix. The functions with the _l suffix will use the locale parameter passed in for their locale information during the execution of that specific function only. To create a locale for use with a function with a _l suffix, use _create_locale. To free this locale, use _free_locale. To get the current locale, use _get_current_locale. Use _configthreadlocale to control whether each thread has its own locale, or all threads in a program share the same locale. For more information, see Locales and Code Pages. More secure versions of the functions in the following table are available, indicated by the _s (“secure”) suffix. For more information, see Security Enhancements in the CRT. Routine See Also |
|
#include <locale.h>
. . . setlocale(LC_ALL,”Chinese-simplified”);//在wcstombs_s之前设置 |
|
size_t convertNum;
size_t wsize = wcslen((const wchar_t*)wstr); size_t asize = wsize * 2 + 1; char* str = (char*)malloc(asize*sizeof(char)); ZeroMemory(str, asize); 宽字符转为多字符: |
|
建议不要用setlocal:
①轻易改变环境变量,容易影响其他程序; ②多线程使用时,依然容易引起乱码,加锁也不行,岂能锁住环境?! ③每次转换都调用,效率低下; |
|
setlocale(LC_ALL,”chs”);
win7 VS2010 WORKED! |
|
WideCharToMultiByte方法确实很好用 |