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

std::mbstowcs

Defined in header <cstdlib>

std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len);

从数组中转换多字节字符串,数组的第一个元素由src它的宽字符表示。所指向的数组中的连续元素中存储转换的字符。dst.不超过len宽字符被写入目标数组。

每个字符被转换为调用std::mbtowc,除非mbTowc转换状态不受影响。在下列情况下,转换将停止:

  • 转换并存储了多字节空字符。
  • 遇到当前C语言环境%29多字节字符中的无效%28。
  • 要存储的下一个宽字符将超过len...

注记

在大多数实现中,该函数更新类型为std::mbstate_t当它通过字符串进行处理时,不能由两个线程同时调用,std::mbsrtowcs在这种情况下应该使用。

POSIX指定一个公共扩展:ifdst为空指针,此函数将返回要写入的宽字符数。dst,如果被转换。相似的行为是标准的std::mbsrtowcs...

参数

dst

-

pointer to wide character array where the wide string will be stored

src

-

pointer to the first element of a null-terminated multibyte string to convert

len

-

number of wide characters available in the array pointed to by dst

返回值

成功后,返回宽字符数,不包括终止字符。L'\0'写入目标数组。

在转换错误%28时,如果遇到无效的多字节字符%29,则返回-1...

二次

代码语言:javascript
复制
#include <iostream>
#include <clocale>
#include <cstdlib>
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    const char* mbstr = u8"z\u00df\u6c34\U0001f34c"; // or u8"zß水?"
                        // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
    wchar_t wstr[5];
    std::mbstowcs(wstr, mbstr, 5);
    std::wcout << "wide string: " << wstr << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
wide string: zß水?

二次

另见

mbsrtowcs

converts a narrow multibyte character string to wide string, given state (function)

wcstombs

converts a wide string to narrow multibyte character string (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 mbstowcs文档

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

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

扫码关注腾讯云开发者

领取腾讯云代金券