Skip to content
Search

WindowsApi:WideCharToMultiByte

lastmod 2025-05-23created 2016-04-094 edits

Converts a Wide Char string to a Multi Byte string. (wchar_t -> char) The function that converts in the opposite direction is MultiByteToWideChar().

A more precise description is as follows.

Maps a UTF-16 (wide character) string to a new character string. The new character string is not necessarily from a multibyte character set. 

Maps a UTF-16 (wide character) string to a new string. The new string does not necessarily have to be a string from a multibyte character set.

Syntax

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
);
  • CodePage: If CP_ACP is used as the first argument, the ANSI code page is used.

Example

Code that converts a commonly used wchar_t (Unicode) to char (ANSI) is as follows.

int mbs_length = wcslen(wcs_string) + 1;
char * convert_mbs = new char [mbs_length];
::WideCharToMultiByte(CP_ACP, 0, wcs_string, -1, convert_mbs, mbs_length, NULL, NULL);
// use convert_mbs varable.
delete [] convert_mbs;

See also

Favorite site