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

std::wstring_convert::to_bytes

Defined in header <locale>

?

?

byte_string to_bytes( Elem wchar );

(1)

?

byte_string to_bytes( const Elem* wptr );

(2)

?

byte_string to_bytes( const wide_string& wstr );

(3)

?

byte_string to_bytes( const Elem* first, const Elem* last);

(4)

?

使用codecvt建筑方面供应。

1%29名皈依者wchar好像是一串长度1,到byte_string...

2%29转换以宽字符开始的空结束宽字符序列。wptr,到byte_string...

3%29转换宽字符串。strbyte_string...

4%29转换宽字符序列[first, last)byte_string...

在所有情况下,转换都以初始移位状态开始,除非为此提供了非初始启动状态。wstring_convert构造函数。转换的字符数和转换状态的最终值将被记住,并且可以用state()converted()...

返回值

byte_string对象,该对象包含宽到多字节转换的结果。如果转换失败并且有用户提供的字节错误字符串提供给wstring_convert,返回字节错误字符串。

例外

如果这个wstring_convert对象是在没有用户提供的字节错误字符串的情况下构造的,引发std::range_error转换失败。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <iomanip>
 
// utility function for output
void hex_print(const std::string& s)
{
    std::cout << std::hex << std::setfill('0');
    for(unsigned char c : s)
        std::cout << std::setw(2) << static_cast<int>(c) << ' ';
    std::cout << std::dec << '\n';
}
 
int main()
{
    // wide character data
    std::wstring wstr =  L"z\u00df\u6c34\U0001f34c"; // or L"z?水?"
 
    // wide to UTF-8
    std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
    std::string u8str = conv1.to_bytes(wstr);
    std::cout << "UTF-8 conversion produced " << u8str.size() << " bytes:\n";
    hex_print(u8str);
 
    // wide to UTF-16le
    std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>> conv2;
    std::string u16str = conv2.to_bytes(wstr);
    std::cout << "UTF-16le conversion produced " << u16str.size() << " bytes:\n";
    hex_print(u16str);
}

二次

产出:

二次

代码语言:javascript
复制
UTF-8 conversion produced 10 bytes:
7a c3 9f e6 b0 b4 f0 9f 8d 8c 
UTF-16le conversion produced 10 bytes:
7a 00 df 00 34 6c 3c d8 4c df

二次

另见

from_bytes

converts a byte string into a wide string (public member function)

wcsrtombs

converts a wide string to narrow multibyte character string, 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)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com