9.13(목) 실습-3(UNICODE)

from Study/API 2007/10/10 11:33 view 19059
// 1. STL에서 사용
typedef
basic_string<char>        string;
typedef basic_string<wchar_t> wstring; int _tmain()
{
    string s;
    basic_string<char> s1 = "hello";
    basic_string<wchar_t> s2 = L"ABCD";
}

// 2. MBCS- > WBCS
int _tmain()
{
    char s[] = "ABCD가나다라";

    // 먼저 s를 wide character로 변경할 때 필요한 버퍼의 크기를 계산한다.
    size_t    sz = MultiByteToWideChar( CP_ACP, 0, s, strlen(s), 0, 0 );

    //이제 버퍼를 할당해서 사용한다.
    wchar_t* s2 = new wchar_t[sz];

    // DBCS(MBCS) -> wideChar 로 변환

    MultiByteToWideChar( CP_ACP,    // 문자열 종류( CP_ACP : ANSI 표준 문자열 )
                         0,
                         s,
                         strlen(s),
                         s2, 10 );

    setlocale( LC_ALL, "Korean" );
    wprintf( s2 );    // wide char를 출력하는 printf, C++의 wcout
    cout << endl;

    delete[] s2;
}

// 3.UNICODE
int _tmain()
{
    TCHAR s[] = _T("ABCD가나다라");

    cout << sizeof(s) << endl;
    cout << _tcslen(s) << endl;
}

// 4. UNICODE 매크로
// 매크로
#define UNICODE

#ifdef UNICODE
typedef wchar_t        TCHAR;
#define _T(x)        L##x    // ## : Token paste 두 Token을 붙인다.
#define TEXT(x)        _T(x)
#define _TEXT(x)    _T(x)
#define _tcslen        wcslen
#define _tmain        wmain

#else
typedef char        TCHAR;
#define _T(x)        x
#define _tcslen        strlen
#define _tmain        main

#endif

int _tmain(int argc, TCHAR* argv)
{
    TCHAR s[] = "ABCD가나다라";

    cout << sizeof( s ) << endl;
    cout << _tcslen(s)    << endl;
}

// 4. WBCS( 2바이트로 인식 )
int main()
{
    wchar_t s[] = L"ABCD가나다라";

    cout << sizeof(s) << endl;    // 18 (null 문자도 2byte )

    cout << strlen((char*)s) << endl;

    cout << wcslen(s) << endl;
}
// 5. DBCS
int main()
{
    char s[] = "ABCD가나다라";

    cout << sizeof(s) << endl;    // ?

    char* p = s;

    while(*p)
    {
        cout << p << endl;

        // p = p + 1;    // 단순 포인터 연산을 사용한 경우

        p = CharNext(p);    // DBCS 관련함수를 통해서 다음 글자 얻기
    }
    return 0;
}
Tag | ,

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다