본문 바로가기

프로그래밍

Unicode 사용

Unicode 를 사용하기 위해서는 _UNICODE 매크로가 정의 되어 있어야 한다.

Visual Studio 에서 _UNICODE 매크로는 프로젝트 속성에 정의 되어있다.

[속성]->[구성속성]->[일반]->[문자집합] : 유니코드 문자 집합 사용

유니코드 사용이 기본값 으로 되어있다.

그리고 다음의 헤더를 포함시킨다.

#include <tchar.h>

tchar.h 은 대충

#ifdef _UNICODE
#define _tprintf wprintf
#define _sntprintf _snwprintf
#define _ttoi atoi

#define TCHAR wchar_t
#define _T( x ) L##x

#else
#define _tprintf printf
#define _sntprintf _snprintf
#define _ttoi _wtoi

#define TCHAR char
#define _T( x ) x

#endif

이런 기존 멀티바이트 문자 가 호환되는 매크로를 정의하고 있다고 한다.

문자열 데이터 타입은 TCHAR 을 사용하고

문자열은 _T("Hello") 이런식으로 사용한다.

그러나 STL의 string의 경우 _UNICODE 매크로와 대응되는 정의가 없으므로 개발자가 직접 작성해 줘야 한다.

예를 들어 이런식으로

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

아래와 같이 정의 하는것도 좋은듯 하다.

#ifdef _UNICODE
namespace std
{
    typedef basic_string<wchar_t>            tstring;
    typedef basic_istream<wchar_t>            tistream;
    typedef basic_ostream<wchar_t>            tostream;
    typedef basic_fstream<wchar_t>            tfstream;
    typedef basic_ifstream<wchar_t>            tifstream;
    typedef basic_ofstream<wchar_t>            tofstream;
    typedef basic_stringstream<wchar_t>        tstringstream;
    typedef basic_istringstream<wchar_t>    tistringstream;
    typedef basic_ostringstream<wchar_t>    tostringstream;
}
#else
namespace std
{
    typedef basic_string<char>                tstring;
    typedef basic_istream<char>                tistream;
    typedef basic_ostream<char>                tostream;
    typedef basic_fstream<char>                tfstream;
    typedef basic_ifstream<char>            tifstream;
    typedef basic_ofstream<char>            tofstream;
    typedef basic_stringstream<char>        tstringstream;
    typedef basic_istringstream<char>        tistringstream;
    typedef basic_ostringstream<char>        tostringstream;
}
#endif

*참고글: 게임개발포에버

'프로그래밍' 카테고리의 다른 글

FormatMessage (Win32)  (0) 2012.12.05
include 가드  (0) 2012.12.05
#define WIN32_LEAN_AND_MEAN  (0) 2012.12.05
gtest (google test) vs2010 설정  (0) 2012.12.04
VS2010 lib 사용 설정  (0) 2012.12.03