简体   繁体   中英

How to convert char* into std::u8string?

Intro

If I catch an exception, I want to convert the error message, which is returned as a C-style string by the what() method, into a std::u8string (a UTF-8 string). For example: std::u8string(error.what());

Problem

How can I convert a char* into a std::u8string ?

Additional Information

  • I only catch exceptions from the standard library, boost and eigen.
  • My application is Windows dependent, so the solution doesn't need to be portable.

You can use the constructor that takes a beginning and an ending iterator for the sequence that defines the string.

#include <cstring>

// ...

auto cstr=error.what();

std::u8string str{cstr, cstr+strlen(cstr)};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM