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());
How can I convert a char*
into a std::u8string
?
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)};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With