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 :指向解释多字节字符串时使用的转换状态的指针
返回值:该函数返回四个值,如下所示:
- 如果为null宽字符或pmb为null指针,则该函数返回0
- 从s成功转换的多字节字符的字节数[1…n]
- 如果pmb的最大前几个字符形成不完整的多字节字符,则该函数返回length-2
- 否则,函数返回length-1并将errno设置为EILSEQ
注意:可能返回的值都不小于零。
以下示例程序旨在说明上述函数:
示例1:
// C++ program to illustrate
// mbrtowc() function
#include <bits/stdc++.h>
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 <bits/stdc++.h>
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++ log()用法及代码示例
- C++ div()用法及代码示例
- C++ fma()用法及代码示例
- C++ map key_comp()用法及代码示例
- C++ real()用法及代码示例
- C++ imag()用法及代码示例
- C++ valarray pow()用法及代码示例
- C++ regex_iterator()用法及代码示例
- C++ valarray log()用法及代码示例
注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 mbrtowc() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。