std::mbrtowc
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <cwchar> 中定义
|
||
std::size_t mbrtowc( wchar_t* pwc, const char* s, |
||
。一个狭窄的多字节字符转换为宽字符.
原文:
Converts a narrow multibyte character to a wide character.
。如果
s
是不是一个空指针,检查n
字节的多字节字符串的开始,指向的字节s
必要去完成下一个多字节字符(包括任何移位序列)的字节数来确定。如果该函数确定下一个多字节字符s
是完整的,有效的,将其转换到对应的宽字符,并将其存储在*pwc(如果pwc
不为null)..原文:
If
s
is not a null pointer, inspects at most n
bytes of the multibyte character string, beginning with the byte pointed to by s
to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in s
is complete and valid, converts it to the corresponding wide character and stores it in *pwc (if pwc
is not null).。
s
是一个空指针,n
和pwc
将被忽略,并呼吁相当于std::mbrtowc(NULL, "", 1, ps).原文:
If
s
is a null pointer, the values of n
and pwc
are ignored and call is equivalent to std::mbrtowc(NULL, "", 1, ps).。如果宽字符是空字符,转换状态存储在*ps是初始位移状态.
原文:
If the wide character produced is the null character, the conversion state stored in *ps is the initial shift state.
[编辑] 。参数。
pwc | - | 。所得的宽字符将被写入的位置的指针。
原文: pointer to the location where the resulting wide character will be written |
s | - | 。指针的多字节字符串作为输入。
原文: pointer to the multibyte character string used as input |
n | - | 。限制的字节数,可以检查。
原文: limit on the number of bytes in s that can be examined |
ps | - | 。指针解释的多字节字符串时使用的转换状态。
原文: pointer to the conversion state used when interpreting the multibyte string |
===。 返回值。===
。以下适用:第一。
原文:
The first of the following that applies:
- 。 0如果从
s
(字符转换,并存储在pwc如果非空)是空字符。原文:0 if the character converted froms
(and stored in pwc if non-null) was the null character - 。从[1...n]成功转换的多字节字符的字节数
s
。原文:the number of bytes [1...n] of the multibyte character successfully converted froms
- 。 static_cast<std::size_t>(-2)
n
如果下一个字节构成一个不完整的,但到目前为止,有效的多字节字符。没有被写入*pwc.原文:static_cast<std::size_t>(-2) if the nextn
bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to *pwc. - 。 static_cast<std::size_t>(-1)如果出现编码错误。不写入任何内容
*pwc
,被存储的值EILSEQerrno和未指定的值*ps.原文:static_cast<std::size_t>(-1) if encoding error occurs. Nothing is written to*pwc
, the value EILSEQ is stored in errno and the value of *ps is left unspecified.
[编辑] 。为例。
#include <iostream> #include <clocale> #include <cstring> #include <cwchar> void print_mb(const char* ptr) { std::mbstate_t state = std::mbstate_t(); // initial state const char* end = ptr + std::strlen(ptr); int len; wchar_t wc; while((len = std::mbrtowc(&wc, ptr, end-ptr, &state)) > 0) { std::wcout << "Next " << len << " bytes are the character " << wc << '\n'; ptr += len; } } int main() { std::setlocale(LC_ALL, "en_US.utf8"); // UTF-8 narrow multibyte encoding const char* str = u8"z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; print_mb(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 𝄋
[编辑] 。另请参阅。
未来的多字节字符转换为宽字符 原文: converts the next multibyte character to wide character (函数) | |
一个宽字符转换为多字节表示,给定的状态 原文: converts a wide character to its multibyte representation, given state (函数) | |
[虚]</div></div>
|
将字符串转换,如从文件读取时,从externT到Internt的 原文: converts a string from externT to internT, such as when reading from file (虚拟保护成员函数of std::codecvt )
|
C documentation for mbrtowc
|