首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::mbrtowc

Defined in header <cwchar>

std::size_t mbrtowc( wchar_t* pwc, const char* s, std::size_t n, std::mbstate_t* ps );

将窄多字节字符转换为宽字符。

如果s不是空指针,最多检查n多字节字符串的字节,以s若要确定完成下一个多字节字符%28(包括任何移位序列%29)所需的字节数,请执行以下操作。中的下一个多字节字符。s是完整和有效的,将其转换为相应的宽字符并将其存储在*pwc%28pwc不是空%29。

如果s为空指针,其值为npwc被忽略,并且调用等效于std::mbrtowc(NULL, "", 1, ps)...

如果产生的宽字符是空字符,则转换状态存储在*ps是初始移位状态。

参数

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

返回值

适用的第一条规定如下:

  • ​0​如果字符转换为s%28并储存在pwc如果非空%29是空字符。
  • 字节数[1...n]成功转换的多字节字符的s
  • static_cast<std::size_t>(-2)如果下一个n字节构成一个不完整的,但迄今为止有效的多字节字符。什么都没有写到*pwc...
  • static_cast<std::size_t>(-1)如果出现编码错误。什么都没有写到*pwc,价值EILSEQ存储在errno的价值*ps未指定。

二次

代码语言:javascript
复制
#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);
}

二次

产出:

二次

代码语言:javascript
复制
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 ?

二次

另见

mbtowc

converts the next multibyte character to wide character (function)

wcrtomb

converts a wide character to its multibyte representation, given state (function)

do_in virtual

converts a string from externT to internT, such as when reading from file (virtual protected member function of std::codecvt)

c.mbrTowc文件c

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券