C++における文字列操作--幅の狭い文字変換の例の詳細

2052 ワード

C++における文字列操作--幅の狭い文字変換の例の詳細
MultiByteToWideChar

 int MultiByteToWideChar(
  _In_   UINT  CodePage,
  _In_   DWORD dwFlags,
  _In_   LPCSTR lpMultiByteStr,
  _In_   int  cbMultiByte,
  _Out_opt_ LPWSTR lpWideCharStr,
  _In_   int  cchWideChar
 );
     :
  CodePage:  CP_ACP、CP_UTF8
  dwFlags:0
  lpMultiByteStr [in]:
            。
  cbMultiByte [in]:
    lpMultiByteStr "       "   。
       0,    ;
       -1,         ,  \0   ,          \0  ,        \0   ;
       >0,      \0,           。
  lpWideCharStr [out, optional]:
                。
  cchWideChar [in]:
    lpWideCharStr       "       "   。
       0,  lpWideCharStr   ,         "       "   。

Code:

 int requiredBufSize = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
 if (requiredBufSize > 0)
 {
   WCHAR *pBuffer = new WCHAR[requiredBufSize];
   MultiByteToWideChar(CP_ACP, 0, src, -1, pBuffer, requiredBufSize);
 }

WideCharToMultiByte

 int WideCharToMultiByte(
  _In_   UINT  CodePage,
  _In_   DWORD  dwFlags,
  _In_   LPCWSTR lpWideCharStr,
  _In_   int   cchWideChar,
  _Out_opt_ LPSTR  lpMultiByteStr,
  _In_   int   cbMultiByte,
  _In_opt_ LPCSTR lpDefaultChar,
  _Out_opt_ LPBOOL lpUsedDefaultChar
 );
     :
  lpDefaultChar [in, optional]:NULL
  lpUsedDefaultChar [out, optional]:NULL
         MultiByteToWideChar

Code:

 int requiredBufSize = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
 if (requiredBufSize > 0)
 {
   char *pBuffer = new char[requiredBufSize];
   WideCharToMultiByte(CP_ACP, 0, src, -1, pBuffer, requiredBufSize, NULL, NULL);
 }

もし疑問があれば伝言を残してあるいは当駅のコミュニティに行って討論を交流して、読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!