Home > database >  How to convert char* into std::u8string?
How to convert char* into std::u8string?

Time:12-09

Intro

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

Problem

How I convert a char* into 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.

CodePudding user response:

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)};
  • Related