Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::wctomb

      From cppreference.com
      <cpp‎ |string‎ |multibyte
       
       
       
       
      Defined in header<cstdlib>
      int wctomb(char* s,wchar_t wc);

      Converts a wide characterwc to multibyte encoding and stores it (including any shift sequences) in the char array whose first element is pointed to bys. No more thanMB_CUR_MAX characters are stored. The conversion is affected by the current locale's LC_CTYPE category.

      Ifwc is the null character, the null byte is written tos, preceded by any shift sequences necessary to restore the initial shift state.

      Ifs is a null pointer, resets the global conversion state and determines whether shift sequences are used.

      Contents

      [edit]Parameters

      s - pointer to the character array for output
      wc - wide character to convert

      [edit]Return value

      Ifs is not a null pointer, returns the number of bytes that are contained in the multibyte representation ofwc or-1 ifwc is not a valid character.

      Ifs is a null pointer, resets its internal conversion state to represent the initial shift state and returns0 if the current multibyte encoding is not state-dependent (does not use shift sequences) or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).

      [edit]Notes

      Each call towctomb updates the internal global conversion state (a static object of typestd::mbstate_t, only known to this function). If the multibyte encoding uses shift states, this function is not reentrant. In any case, multiple threads should not callwctomb without synchronization:std::wcrtomb may be used instead.

      [edit]Example

      Run this code
      #include <clocale>#include <cstdlib>#include <iomanip>#include <iostream>#include <string> void print_wide(conststd::wstring& wstr){bool shifts= std::wctomb(nullptr,0);// reset the conversion statestd::cout<<"shift sequences are "<<(shifts?"":"not")<<" used\n"<<std::uppercase<<std::setfill('0');for(constwchar_t wc: wstr){std::string mb(MB_CUR_MAX,'\0');constint ret= std::wctomb(&mb[0], wc);constchar* s= ret>1?"s":"";std::cout<<"multibyte char '"<< mb<<"' is "<< ret<<" byte"<< s<<": ["<<std::hex;for(int i{0}; i!= ret;++i){constint c=0xFF& mb[i];std::cout<<(i?" ":"")<<std::setw(2)<< c;}std::cout<<"]\n"<<std::dec;}} int main(){std::setlocale(LC_ALL,"en_US.utf8");// UTF-8 narrow multibyte encodingstd::wstring wstr= L"z\u00df\u6c34\U0001d10b";// or L"zß水𝄋"    print_wide(wstr);}

      Output:

      shift sequences are not usedmultibyte char 'z' is 1 byte: [7A]multibyte char 'ß' is 2 bytes: [C3 9F]multibyte char '水' is 3 bytes: [E6 B0 B4]multibyte char '𝄋' is 4 bytes: [F0 9D 84 8B]

      [edit]See also

      converts the next multibyte character to wide character
      (function)[edit]
      converts a wide character to its multibyte representation, given state
      (function)[edit]
      [virtual]
      converts a string fromInternT toExternT, such as when writing to file
      (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>)[edit]
      C documentation forwctomb
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/multibyte/wctomb&oldid=153684"

      [8]ページ先頭

      ©2009-2025 Movatter.jp