Share via


mbrtowc

將目前地區設定的多位元組字元轉換成對等的寬字元,並提供在多位元組字元中間重新啟動的功能。

語法

size_t mbrtowc(
   wchar_t *wchar,
   const char *mbchar,
   size_t count,
   mbstate_t *mbstate
);

參數

wchar
要接收已轉換寬字元字串 (類型 wchar_t) 的寬字元位址。 如果不需要傳回任何寬字元,這個值可以是 null 指標。

mbchar
位元組序列 (多位元組字元) 的位址。

count
要檢查的位元組數目。

mbstate
轉換狀態物件的指標。 如果這個值是 null 指標,函式會使用靜態內部轉換狀態物件。 由於內部 mbstate_t 物件不是安全線程,因此建議您一律傳遞自己的 mbstate 引數。

傳回值

下列其中一個值:

0 下一個 count 或更少的位元組會完成代表 Null 寬字元的多位元組字元,如果 wchar 不是 Null 指標,則儲存在 wchar 中。

1 到 count ,包含下一 count 個或更少的位元組完成有效的多位元組字元。 傳回的值是完成多位元組字元的位元組數目。 如果 wchar 不是 Null 指標,則寬字元對等專案會儲存在 wchar 中。

(size_t)(-1)發生編碼錯誤。 下一 count 個或更少的位元組不會造成完整且有效的多位元組字元。 在這個情況下,errno 會設為 EILSEQ,且 mbstate 中的轉換移位狀態為未指定。

(size_t)(-2)下一個 count 位元組會參與不完整但可能有效的多位元組字元,而且所有 count 位元組都已處理。 沒有值儲存在 wchar 中,但更新了 mbstate 以重新啟動函式。

備註

如果 mbchar 是 null 指標,函式相當於呼叫:

mbrtowc(NULL, "", 1, &mbstate)

在此情況下,會忽略 和 count 引數的值 wchar

如果 mbchar 不是 Null 指標,函式會 count 檢查 的 mbchar 位元組,以判斷完成下一個多位元組字元所需的位元組數目。 如果下一個字元有效,如果不是 Null 指標,則對應的多位元組字元會儲存在 中 wchar 。 如果字元是對應的 null 寬字元,產生的 mbstate 狀態是初始轉換狀態。

函式 mbrtowc_mbtowc_lmbtowc 不同之處在于其可重新開機性。 針對相同或其他可重新啟動的函式的後續呼叫,轉換狀態會儲存在 mbstate 中。 混合使用可重新啟動和不可重新啟動之函式的結果不明。 例如,如果使用 wcsrlen 的後續呼叫,而不是 wcslen,應用程式應該使用 wcsrtombs,而不是 wcstombs

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

範例

將多位元組字元轉換成其對等的寬字元。

// crt_mbrtowc.cpp

#include <stdio.h>
#include <mbctype.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>

#define BUF_SIZE 100

int Sample(char* szIn, wchar_t* wcOut, int nMax)
{
    mbstate_t   state = {0}; // Initial state
    size_t      nConvResult,
                nmbLen = 0,
                nwcLen = 0;
    wchar_t*    wcCur = wcOut;
    wchar_t*    wcEnd = wcCur + nMax;
    const char* mbCur = szIn;
    const char* mbEnd = mbCur + strlen(mbCur) + 1;
    char*       szLocal;

    // Sets all locale to French_Canada.1252
    szLocal = setlocale(LC_ALL, "French_Canada.1252");
    if (!szLocal)
    {
        printf("The fuction setlocale(LC_ALL, \"French_Canada.1252\") failed!\n");
        return 1;
    }

    printf("Locale set to: \"%s\"\n", szLocal);

    // Sets the code page associated current locale's code page
    // from a previous call to setlocale.
    if (_setmbcp(_MB_CP_SBCS) == -1)
    {
        printf("The fuction _setmbcp(_MB_CP_SBCS) failed!");
        return 1;
    }

    while ((mbCur < mbEnd) && (wcCur < wcEnd))
    {
        //
        nConvResult = mbrtowc(wcCur, mbCur, 1, &state);
        switch (nConvResult)
        {
            case 0:
            {  // done
                printf("Conversion succeeded!\nMultibyte String: ");
                printf(szIn);
                printf("\nWC String: ");
                wprintf(wcOut);
                printf("\n");
                mbCur = mbEnd;
                break;
            }

            case -1:
            {  // encoding error
                printf("The call to mbrtowc has detected an encoding error.\n");
                mbCur = mbEnd;
                break;
            }

            case -2:
            {  // incomplete character
                if   (!mbsinit(&state))
                {
                    printf("Currently in middle of mb conversion, state = %x\n", state);
                    // state will contain data regarding lead byte of mb character
                }

                ++nmbLen;
                ++mbCur;
                break;
            }

            default:
            {
                if   (nConvResult > 2) // The multibyte should never be larger than 2
                {
                    printf("Error: The size of the converted multibyte is %d.\n", nConvResult);
                }

                ++nmbLen;
                ++nwcLen;
                ++wcCur;
                ++mbCur;
            break;
            }
        }
    }

   return 0;
}

int main(int argc, char* argv[])
{
    char    mbBuf[BUF_SIZE] = "AaBbCc\x9A\x8B\xE0\xEF\xF0xXyYzZ";
    wchar_t wcBuf[BUF_SIZE] = {L''};

    return Sample(mbBuf, wcBuf, BUF_SIZE);
}

範例輸出

Locale set to: "French_Canada.1252"
Conversion succeeded!
Multibyte String: AaBbCcÜïα∩≡xXyYzZ
WC String: AaBbCcÜïα∩≡xXyYzZ

需求

常式 必要的標頭
mbrtowc <wchar.h>

另請參閱

資料轉換
地區設定
多位元組字元序列的解譯