Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,817 answers

573 users

How to convert 16-bit wide character (UTF-16) to its narrow multibyte character (UTF-8) in C++

1 Answer

0 votes
#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 ]
  
*/
 

 



answered Dec 18, 2024 by avibootz
...