[WinAPI] MultiByteToWideChar, WideCharToMultiByte 사용 예제

MultiByte, WideChar 변환에 대해 알아보겠습니다.

1. MultiByte에서 WideChar 변환
int MultiByteToWideChar(
  UINT                              CodePage, // 코드페이지(CP_ACP)
  DWORD                             dwFlags, // 플래그
  _In_NLS_string_(cbMultiByte)LPCCH lpMultiByteStr, // MultiByte
  int                               cbMultiByte, // MultiByte 길이
  LPWSTR                            lpWideCharStr, // WideChar
  int                               cchWideChar // WideChar 크기
);

사용 예제

wchar_t strUnicode[256] = {0,}; 
char strMultibyte[256] = {0,}; 
strcpy_s(strMultibyte,256,"멀티바이트"); 
int nLen = MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), NULL, NULL); 
MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), strUnicode, nLen);

2. WideChar에서 MultiByte 변환
int WideCharToMultiByte(
  UINT                               CodePage, // 코드페이지(CP_ACP)
  DWORD                              dwFlags, // 플래그
  _In_NLS_string_(cchWideChar)LPCWCH lpWideCharStr, // WideChar
  int                                cchWideChar, // WideChar 크기
  LPSTR                              lpMultiByteStr, // MultiByte
  int                                cbMultiByte, // MultiByte 크기
  LPCCH                              lpDefaultChar, // 디폴드 문자열
  LPBOOL                             lpUsedDefaultChar // 플래그를 가리키는 포인터
);

사용 예제

wchar_t strUnicode[256] = {0,}; 
char strMultibyte[256] = {0,}; 
wcscpy_s(strUnicode,256,L"유니코드"); 
int len = WideCharToMultiByte( CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL );	
WideCharToMultiByte( CP_ACP, 0, strUnicode, -1, strMultibyte, len, NULL, NULL );