#include <climits> // MB_LEN_MAX
#include <clocale>
#include <cuchar> // c16rtomb
#include <iostream>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::u16string_view u16str = u"aßb£c水d";
std::cout << "Processing " << u16str.size() << " UTF-16 code units: [ ";
for (char16_t c : u16str)
std::cout << std::showbase << std::hex << static_cast<int>(c) << ' ';
std::cout << "] into UTF-8 code units:\n";
std::mbstate_t state{};
char out[MB_LEN_MAX]{};
for (char16_t ch : u16str) {
std::size_t byteswritten = std::c16rtomb(out, ch, &state);
std::cout << static_cast<int>(ch) << " converted to [ ";
if (byteswritten != (std::size_t) - 1) {
for (unsigned char c8 : std::string_view{out, byteswritten}) {
std::cout << +c8 << ' ';
}
}
std::cout << "]\n";
}
}
/*
run:
Processing 7 UTF-16 code units: [ 0x61 0xdf 0x62 0xa3 0x63 0x6c34 0x64 ] into UTF-8 code units:
0x61 converted to [ 0x61 ]
0xdf converted to [ 0xc3 0x9f ]
0x62 converted to [ 0x62 ]
0xa3 converted to [ 0xc2 0xa3 ]
0x63 converted to [ 0x63 ]
0x6c34 converted to [ 0xe6 0xb0 0xb4 ]
0x64 converted to [ 0x64 ]
*/