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

std::wstring_convert::from_bytes

Defined in header <locale>

?

?

wide_string from_bytes( char byte );

(1)

?

wide_string from_bytes( const char* ptr );

(2)

?

wide_string from_bytes( const byte_string& str );

(3)

?

wide_string from_bytes( const char* first, const char* last);

(4)

?

使用构造时提供的codecvt面执行多字节到宽转换。

1%29名皈依者byte好像是一串长度1变宽[医]绳子。

2%29转换以ptr变宽[医]绳子。

3%29转换窄字符串str变宽[医]绳子。

4%29转换窄的多字节字符序列。[first, last)变宽[医]绳子。

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

返回值

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

例外

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

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
 
int main()
{
    std::string utf8 =  u8"z\u00df\u6c34\U0001d10b"; // or u8"z?水?"
                        // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
 
    // the UTF-8 / UTF-16 standard conversion facet
    std::u16string utf16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(utf8.data());
    std::cout << "UTF16 conversion produced " << utf16.size() << " code units:\n";
    for (char16_t c : utf16)
        std::cout << std::hex << std::showbase << c << '\n';
 
    // the UTF-8 / UTF-32 standard conversion facet
    std::u32string utf32 = std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.from_bytes(utf8);
    std::cout << "UTF32 conversion produced " << std::dec << utf32.size() << " code units:\n";
    for (char32_t c : utf32)
        std::cout << std::hex << std::showbase << c << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
UTF16 conversion produced 5 code units:
0x7a
0xdf
0x6c34
0xd834
0xdd0b
UTF32 conversion produced 4 code units:
0x7a
0xdf
0x6c34
0x1d10b

二次

另见

to_bytes

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

mbsrtowcs

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com