wcstombs()

Ganz nützlich, wenn man UTF-16 kodierte Zeichen im Terminal darstellen möchte:

$ cat -n main.cpp
     1	#include <stdint.h>
     2	#include <iostream>
     3	#include <iomanip>
     4	#include <cstdlib>
     5	#include <vector>
     6	
     7	int main( int argc, char** argv )
     8	{
     9	  // Some blocks.
    10	  std::wstring blocks( L"\u2581\u2582\u2583\u2584\u2585" );
    11	
    12	  // Does not work.
    13	  //std::wcout << blocks << std::endl;
    14	
    15	  // It's UTF-16, convert to multibyte string.
    16	
    17	  // Prepare destination.
    18	  uint16_t bytes = ( blocks.length() * sizeof( wchar_t ) ) + 1;
    19	  std::vector<char> dest( bytes );
    20	
    21	  // Setting locale is required, otherwise wcstombs() does not work.
    22	  std::setlocale( LC_ALL, "C.UTF-8" );
    23	
    24	  // Convert.
    25	  uint16_t rc = std::wcstombs( &( dest[ 0 ] ), blocks.c_str(), bytes );
    26	  std::cout << "wcstombs() wrote " << rc << " bytes" << std::endl;
    27	
    28	  // Build string and print.
    29	  std::string result( &( dest[ 0 ] ) );
    30	  std::cout << "printing as multibyte string does the trick: ";
    31	  std::cout << result << std::endl;
    32	}