共用方式為


mbstowcs _mbstowcs_l

將相對應的一連串的寬字元轉換為多位元組字元序列。 這些函式更安全版本都可使用; see mbstowcs_s _mbstowcs_s_l.

size_t mbstowcs(
   wchar_t *wcstr,
   const char *mbstr,
   size_t count 
);
size_t _mbstowcs_l(
   wchar_t *wcstr,
   const char *mbstr,
   size_t count,
   _locale_t locale
);
template <size_t size>
size_t mbstowcs(
   wchar_t (&wcstr)[size],
   const char *mbstr,
   size_t count 
); // C++ only
template <size_t size>
size_t _mbstowcs_l(
   wchar_t (&wcstr)[size],
   const char *mbstr,
   size_t count,
   _locale_t locale
); // C++ only

參數

  • [] outwcstr
    寬字元序列的地址。

  • [in]mbstr
    Null 的一系列的地址終止多位元組字元。

  • [in] count
    若要轉換的多位元組字元的數目上限。

  • [in] locale
    若要使用地區設定。

傳回值

如果mbstowcs成功轉換來源的字串,則會傳回已轉換的多位元組字元數。 如果wcstr引數是NULL,函式會傳回目的字串所需的大小 (以寬字元為單位)。 如果mbstowcs遇到了無效的多位元組字元,則會傳回 – 1。 如果傳回值是count,寬字元字串不是 null 值結束。

安全性注意事項安全性提示

確保wcstr和mbstr不會重疊,且該count正確地反映出要轉換的多位元組字元數。

備註

mbstowcs進位至最大數目的函式將轉換count所指多位元組字元mbstr為對應由目前的地區設定的寬字元字串。 它會儲存在所代表的地址產生的寬字元字串wcstr*.* 結果就像一系列呼叫mbtowc。 如果mbstowcs之前或當遇到半形的 null 字元 ('\ 0') count ,就會發生,它會將轉換為寬字元的 null 字元 (L '\ 0') 的 null 字元並停駐點。 因此在寬字元字串wcstr是 null 結尾只有當在轉換過程中遇到一個 null 字元。 如果所指的序列wcstr和mbstr相互重疊,這個行為未定義。

如果wcstr引數是NULL, mbstowcs傳回會因轉換時,不包括 null 結束字元的寬字元數目。 在來源字串必須是正確的值,傳回 null 值結束。 如果您需要產生的寬字元字串,以 null 結束,加入至傳回的值。

If the mbstr argument is NULL, or if count is > INT_MAX無效的參數處理常式會叫用,如所述參數驗證 。 如果執行,則允許繼續執行,errno 會設定為EINVAL ,則函數會傳回-1。

mbstowcs使用目前的地區設定的任何地區設定相關的行為。 _mbstowcs_l 不同之處在於它所使用的地區設定中傳遞,則是完全相同。 如需詳細資訊,請參閱 地區設定

在 C++ 中,這些函式會有範本的多載,叫用這些函式的較新的、 安全對應項目。 如需詳細資訊,請參閱 安全範本多載

需求

常式

所需的標頭

mbstowcs

<stdlib.h>

_mbstowcs_l

<stdlib.h>

其他的相容性資訊,請參閱相容性在簡介中。

範例

// crt_mbstowcs.c
// compile with: /W3
// illustrates the behavior of the mbstowcs function

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

int main( void )
{
    size_t size;
    int nChar = 2; // number of characters to convert
    int requiredSize;

    unsigned char    *pmbnull  = NULL;
    unsigned char    *pmbhello = NULL;
    char* localeInfo;
    
    wchar_t *pwchello = L"\x3042\x3043"; // 2 Hiragana characters
    wchar_t *pwc;

    /* Enable the Japanese locale and codepage */
    localeInfo = setlocale(LC_ALL, "Japanese_Japan.932");
    printf("Locale information set to %s\n", localeInfo);
    
    printf( "Convert to multibyte string:\n" );

    requiredSize = wcstombs( NULL, pwchello, 0); // C4996
    // Note: wcstombs is deprecated; consider using wcstombs_s
    printf("  Required Size: %d\n", requiredSize);

    /* Add one to leave room for the null terminator. */
    pmbhello = (unsigned char *)malloc( requiredSize + 1);
    if (! pmbhello)
    {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = wcstombs( pmbhello, pwchello, requiredSize + 1); // C4996
    // Note: wcstombs is deprecated; consider using wcstombs_s
    if (size == (size_t) (-1))
    {
        printf("Couldn't convert string. Code page 932 may"
                " not be available.\n");
        return 1;
    }
    printf( "  Number of bytes written to multibyte string: %u\n",
            (unsigned int) size );
    printf( "  Hex values of the " );
    printf( " multibyte characters: %#.2x %#.2x %#.2x %#.2x\n",
            pmbhello[0], pmbhello[1], pmbhello[2], pmbhello[3] );
    printf( "  Codepage 932 uses 0x81 to 0x9f as lead bytes.\n\n");

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

    /* Assume we don't know the length of the multibyte string.
     Get the required size in characters, and allocate enough space. */

    requiredSize = mbstowcs(NULL, pmbhello, 0); // C4996
    /* Add one to leave room for the NULL terminator */
    pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t ));
    if (! pwc)
    {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = mbstowcs( pwc, pmbhello, requiredSize + 1); // C4996
    if (size == (size_t) (-1))
    {
       printf("Couldn't convert string--invalid multibyte character.\n");
    }
    printf( "  Characters converted: %u\n", (unsigned int)size );
    printf( "  Hex value of first 2" );
    printf( " wide characters: %#.4x %#.4x\n\n", pwc[0], pwc[1] );
    free(pwc);
    free(pmbhello);
}
      

.NET Framework 對等用法

不適用。 若要呼叫標準的 c 函式,使用PInvoke。 如需詳細資訊,請參閱平台叫用範例

請參閱

參考

資料轉換

地區設定

多位元組字元序列的轉譯工作

_mbclen,mblen _mblen_l

mbtowc _mbtowc_l

wcstombs _wcstombs_l

wctomb _wctomb_l

MultiByteToWideChar