Skip to content

Instantly share code, notes, and snippets.

@iboB
Created October 3, 2021 10:16
Show Gist options
  • Save iboB/399221e767e703e2befa17bd411faea3 to your computer and use it in GitHub Desktop.
Save iboB/399221e767e703e2befa17bd411faea3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <locale>
#include <string>
struct cvt32_8 : public std::codecvt<char32_t, char, std::mbstate_t>
{};
int main()
{
cvt32_8 cvt;
char32_t str[] = U"konzele";
std::mbstate_t mb;
for (auto c : str)
{
char buf[5];
const char32_t* end32;
char* end8;
cvt.out(mb, &c, &c + 1, end32, buf, buf + 4, end8);
*end8 = 0;
std::cout << buf;
}
std::cout << '\n';
std::string foo = "21312";
const char* fbeg = foo.data();
const auto fe = fbeg + foo.size();
for (auto c : foo)
{
char32_t out;
char32_t* u;
cvt.in(mb, fbeg, fe, fbeg, &out, &out + 1, u);
std::cout << out << ", ";
}
std::cout << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment