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

使用wcstombs_s()把WCHAR字串转换成char字串,如何转不了中文

2013-07-01 
使用wcstombs_s()把WCHAR字串转换成char字串,怎么转不了中文?我用msdn的例子来试验:#include stdafx.h#i

使用wcstombs_s()把WCHAR字串转换成char字串,怎么转不了中文?
我用msdn的例子来试验:

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main( void )
{
    size_t   i;
    char   *  pMBBuffer = (char *)malloc( BUFFER_SIZE );
    wchar_t*  pWCBuffer = L"Hello, world.";                             //(1)
    //wchar_t*  pWCBuffer = L"世界, 你好.";  //换成_T("世界, 你好.")也不行 //(2)

    printf( "Convert wide-character string:\n" );

    // Conversion
    wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, 
               pWCBuffer, (size_t)BUFFER_SIZE );

    // Output
    printf("   Characters converted: %u\n", i);
    printf("    Multibyte character: %s\n\n",
     pMBBuffer );

    // Free multibyte character buffer
    if (pMBBuffer)
    {
    free(pMBBuffer);
    }
}

msdn的代码肯定是没事的,但我把上面(1)句注释掉换成(2)句,即字串换成中文,就不行了!打印不出来转换后的文字,设断点查pMBBuffer,发现wcstombs_s调用后pMBBuffer是空的,什么都没转出来。

为什么英文可以中文不行?why?
[解决办法]
调用wcstombs之前先调setlocale(LC_ALL, "zh-CN");否则wcstombs不会处理超过255的字符,不过这到底是bug还是标准就不知道了。
[解决办法]
 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.

 
errno_t wcstombs_s(
   size_t *pReturnValue,
   char *mbstr,
   size_t sizeInBytes,
   const wchar_t *wcstr,
   size_t count 
);
errno_t _wcstombs_s_l(
   size_t *pReturnValue,
   char *mbstr,


   size_t sizeInBytes,
   const wchar_t *wcstr,
   size_t count,
   _locale_t locale
);
template <size_t size>
errno_t wcstombs_s(
   size_t *pReturnValue,
   char (&mbstr)[size],
   const wchar_t *wcstr,
   size_t count 
); // C++ only
template <size_t size>
errno_t _wcstombs_s_l(
   size_t *pReturnValue,
   char (&mbstr)[size],
   const wchar_t *wcstr,
   size_t count,
   _locale_t locale
); // C++ only
 

Parameters
[out] pReturnValue
The number of characters converted.

[out] mbstr
The address of a buffer for the resulting converted multibyte character string.

[in]sizeInBytes
The size in bytes of the mbstr buffer.

[in] wcstr
Points to the wide character string to be converted.

[in] count
The maximum number of bytes to be stored in the mbstr buffer, or _TRUNCATE.

[in] locale
The locale to use.

Return Value
Zero if successful, an error code on failure.

Error condition
 Return value and errno
 
mbstr is NULL and sizeInBytes > 0
 EINVAL
 
wcstr is NULL
 EINVAL
 
The destination buffer is too small to contain the converted string (unless count is _TRUNCATE; see Remarks below)
 ERANGE
 

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
The wcstombs_s function converts a string of wide characters pointed to by wcstr into multibyte characters stored in the buffer pointed to by mbstr. The conversion will continue for each character until one of these conditions is met:

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: 
Ensure that wcstr and mbstr do not overlap, and that count correctly reflects the number of wide characters to convert.
 

wcstombs_s uses the current locale for any locale-dependent behavior; _wcstombs_s_l is identical to wcstombs except that it uses the locale passed in instead. For more information, see Locale.

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.

Requirements
Routine
 Required header
 
wcstombs_s
 <stdlib.h>
 

For additional compatibility information, see Compatibility in the Introduction.

Example
This program illustrates the behavior of the wcstombs_s function.

  Copy Code 
// crt_wcstombs_s.c
// This example converts a wide character
// string to a multibyte character string.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main( void )
{
    size_t   i;
    char      *pMBBuffer = (char *)malloc( BUFFER_SIZE );
    wchar_t*pWCBuffer = L"Hello, world.";

    printf( "Convert wide-character string:\n" );

    // Conversion
    wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, 
               pWCBuffer, (size_t)BUFFER_SIZE );

    // Output
    printf("   Characters converted: %u\n", i);
    printf("    Multibyte character: %s\n\n",
     pMBBuffer );

    // Free multibyte character buffer
    if (pMBBuffer)
    {
    free(pMBBuffer);
    }
}
 
  Copy Code 
Convert wide-character string:
   Characters converted: 14
    Multibyte character: Hello, world.


 

.NET Framework Equivalent
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

See Also
Concepts
Data Conversion
Locale
_mbclen, mblen, _mblen_l
mbstowcs, _mbstowcs_l
mbtowc, _mbtowc_l
wctomb_s, _wctomb_s_l
WideCharToMultiByte
Send feedback on this topic to Microsoft.

热点排行