📜  C / C++中的mbrtowc()函数

📅  最后修改于: 2021-05-25 20:40:11             🧑  作者: Mango

C / C++中的mbrtowc()函数将多字节序列转换为宽字符。此函数返回多字节字符的长度(以字节为单位)。 s指向的多字节字符将转换为wchar_t类型的值,并存储在pwc指向的位置。如果s指向空字符,则该函数将移位状态复位并在将宽的空字符存储在pwc之后返回零。

句法:

size_t mbrtowc (wchar_t* pwc, const char* pmb, size_t max, mbstate_t* ps)

参数:该函数接受四个参数,如下所述:

  • pwc:指向将写入宽字符的位置的指针
  • S:指针多字节字符的字符串用作输入
  • n:可以检查的s中的字节数限制
  • ps:指向解释多字节字符串时使用的转换状态的指针

返回值:该函数返回四个值,如下所示:

  1. 如果为null宽字符或pmb为null指针,则该函数返回0
  2. 从s成功转换的多字节字符的字节数[1…n]
  3. 如果pmb的最大前几个字符形成不完整的多字节字符,则该函数返回length-2
  4. 否则,函数返回length-1,并将errno设置为EILSEQ

注意:可能返回的值都不小于零。

下面的程序说明了上述函数:
程序1:

// C++ program to illustrate
// mbrtowc() function
#include 
using namespace std;
  
// Function to convert multibyte
// sequence to wide character
void print_(const char* s)
{
    // initial state
    mbstate_t ps = mbstate_t();
  
    // length of the string
    int length = strlen(s);
  
    const char* n = s + length;
    int len;
    wchar_t pwc;
  
    // printing each bytes
    while ((len = mbrtowc(&pwc, s, n - s, &ps)) > 0) {
        wcout << "Next " << len << 
        " bytes are the character " << pwc << '\n';
        s += len;
    }
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
  
    // UTF-8 narrow multibyte encoding
    const char* str = u8"z\u00df\u6c34\U0001d10b";
  
    print_(str);
}
输出:
Next 1 bytes are the character z
Next 2 bytes are the character Ã?
Next 3 bytes are the character æ°´
Next 4 bytes are the character ð??

程式2:

// C++ program to illustrate
// mbrtowc() function
// with different UTF-8 characters
#include 
using namespace std;
  
// Function to convert multibyte
// sequence to wide character
void print_(const char* s)
{
    // initial state
    mbstate_t ps = mbstate_t();
  
    // length of the string
    int length = strlen(s);
  
    const char* n = s + length;
    int len;
    wchar_t pwc;
  
    // printing each bytes
    while ((len = mbrtowc(&pwc, s, n - s, &ps)) > 0) {
        wcout << "Next " << len << 
        " bytes are the character " << pwc << '\n';
        s += len;
    }
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
  
    // UTF-8 narrow multibyte encoding
    const char* str = u8"\xE2\x88\x83y\xE2\x88\x80x\xC2\xAC";
  
    print_(str);
}
输出:
Next 3 bytes are the character â??
Next 1 bytes are the character y
Next 3 bytes are the character â??
Next 1 bytes are the character x
Next 2 bytes are the character ¬
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。