std::wctomb
Defined in header <cstdlib> | | |
|---|---|---|
int wctomb( char *s, wchar_t wc ); | | |
转换宽字符wc到多字节编码,并将其存储在%28中,包括字符数组中的任何移位序列%29,其第一个元素由s.不超过MB_CUR_MAX字符被存储。
如果wc为空字符,则将空字节写入s,在此之前有恢复初始移位状态所需的任何移位序列。
如果s为空指针,重置全局转换状态并确定是否使用移位序列。
参数
s | - | pointer to the character array for output |
|---|---|---|
wc | - | wide character to convert |
返回值
如果s的多字节表示中包含的字节数。wc或-1如果wc不是一个有效的字符。
如果s为空指针,将其内部转换状态重置为表示初始移位状态并返回。0如果当前多字节编码不依赖于状态,则%28不使用Shift序列%29,如果当前多字节编码依赖于状态,则%28不使用Shift序列%29,则使用非零值。
注记
每次呼叫wctomb更新内部全局转换状态%28a类型的静态对象std::mbstate_t,只知道此函数%29。如果多字节编码使用Shift状态,则此函数不会重入。在任何情况下,多个线程都不应该调用wctomb如果没有同步:std::wcrtomb可能会被使用。
例
二次
#include <iostream>
#include <clocale>
#include <string>
#include <cstdlib>
void print_wide(const std::wstring& wstr)
{
bool shifts = std::wctomb(NULL, 0); // reset the conversion state
std::cout << "shift sequences " << (shifts ? "are" : "not" ) << " used\n";
for (wchar_t wc : wstr) {
std::string mb(MB_CUR_MAX, '\0');
int ret = std::wctomb(&mb[0], wc);
std::cout << "multibyte char " << mb << " is " << ret << " bytes\n";
}
}
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
// UTF-8 narrow multibyte encoding
std::wstring wstr = L"z\u00df\u6c34\U0001d10b"; // or L"zß水?"
print_wide(wstr);
}二次
产出:
二次
shift sequences not used
multibyte char z is 1 bytes
multibyte char ß is 2 bytes
multibyte char 水 is 3 bytes
multibyte char ? is 4 bytes二次
另见
mbtowc | converts the next multibyte character to wide character (function) |
|---|---|
wcrtomb | converts a wide character to its multibyte representation, given state (function) |
do_out virtual | converts a string from internT to externT, such as when writing to file (virtual protected member function of std::codecvt) |
C文档
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

