Unicode string를 MBCS string으로 바꾸는 방법

1. WideCharToMultiByte() API를 호출하는 방법
2. CRT 함수 wcstombs() 를 호출하는 방법
3. CString 생성자나 assign을 통한 방법(MFC만 가능)
4. ATL string 변환 매크로를 이용하는 방법

WideCharToMultiByte

// Assuming we already have a Unicode string wszSomeString…
char szANSIString [MAX_PATH];

    WideCharToMultiByte ( CP_ACP,                // ANSI code page
                          WC_COMPOSITECHECK,     // Check for accented characters
                          wszSomeString,         // Source Unicode string
                          -1,                    // -1 means string is zero-terminated
                          szANSIString,          // Destination char string
                          sizeof(szANSIString),  // Size of buffer
                          NULL,                  // No default character
                          NULL );                // Don’t care about this flag

wcstombs

    wcstombs ( szANSIString, wszSomeString, sizeof(szANSIString) );

CString

// Assuming we already have wszSomeString…

CString str1 ( wszSomeString );    // Convert with a constructor.
CString str2;

    str2 = wszSomeString;          // Convert with an assignment operator.

ATL Macro

#include <atlconv.h>

// Again assuming we have wszSomeString…

{
char szANSIString [MAX_PATH];
USES_CONVERSION;  // Declare local variable used by the macros.

    lstrcpy ( szANSIString, OLE2A(wszSomeString) );
}

참고 : Introduction
to COM – What It Is and How to Use It.
by Michael Dunn

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다