유니코드 변환

유니코드와 멀티 바이트 코드 간에 변환 하는 방법이 잘 알려져 있지만 매개 변수를 채우는 것이 여간 귀찮은 것이 아니다. 어디서 코드를 가져 왔는지 기억이 안나지만 인터넷에서 가져온 코드다.

소스 : unicode.cpp

//유니코드를 멀티 코드로 변환

std::string wstrtostr(const std::wstring &wstr)

{

    // Convert a Unicode string to an ASCII string

    std::string strTo;

    char *szTo = new char[wstr.length() + 1];

    szTo[wstr.size()] = '\0';

    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);

    strTo = szTo;

    delete[] szTo;

    return strTo;

}

 

//멀티코드를 유니 코드로 변환

std::wstring strtowstr(const std::string &str)

{

    // Convert an ASCII string to a Unicode String

    std::wstring wstrTo;

    wchar_t *wszTo = new wchar_t[str.length() + 1];

    wszTo[str.size()] = L'\0';

    MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo, (int)str.length());

    wstrTo = wszTo;

    delete[] wszTo;

    return wstrTo;

}

 

//유니코드를 UTF8로 변환

int WideToUTF8( const WCHAR* szSrcUnicode, char* szUtf, int destSize )

{

    int nSize = WideCharToMultiByte( CP_UTF8, 0, szSrcUnicode, -1, NULL, 0, NULL, NULL );

    if( destSize < nSize )

        return 0;

    ::WideCharToMultiByte( CP_UTF8, 0, szSrcUnicode, -1, szUtf, nSize, NULL, NULL);

    szUtf[ nSize ] = 0;

    return nSize;

}

 

//UTF8을 유니코드로 변환

int UTF8ToWide( const char* szSrc, WCHAR* szUnicode, int destSize )

{

    int nSize = MultiByteToWideChar(CP_UTF8, 0, szSrc, -1, 0, 0);

    if( destSize < nSize )

        return 0;

    int nUnicodeSize = MultiByteToWideChar(CP_UTF8, 0, szSrc, -1 , szUnicode, nSize);

    szUnicode[ nSize ] = 0;

    return nSize;

}

 

//UTF8을 멀티코드로 변환

int UTF8ToAnsi( const char* szSrc, char* strDest, int destSize )

{

    WCHAR    szUnicode[255];

    char    szAnsi[255];

 

    int nSize = MultiByteToWideChar(CP_UTF8, 0, szSrc, -1, 0, 0);

    int nUnicodeSize = MultiByteToWideChar(CP_UTF8, 0, szSrc, -1 , szUnicode, nSize);

    int nAnsiSize = WideCharToMultiByte(CP_ACP, 0, szUnicode, nUnicodeSize, szAnsi, sizeof( szAnsi ), NULL, NULL);

    assert( destSize > nAnsiSize );

    memcpy(strDest, szAnsi, nAnsiSize );

    strDest[ nAnsiSize ] = 0;

    return nAnsiSize;

}

 

//멀티코드를 UTF8로 변환

int AnsiToUTF8( char* szSrc, char* strDest, int destSize )

{

    WCHAR    szUnicode[255];

    char    szUTF8code[255];

 

    int nUnicodeSize = MultiByteToWideChar(CP_ACP, 0, szSrc, (int)strlen(szSrc), szUnicode, sizeof( szUnicode ));

    int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, szUnicode, nUnicodeSize, szUTF8code, sizeof( szUTF8code ), NULL, NULL);

    assert( destSize > nUTF8codeSize );

    memcpy(strDest, szUTF8code, nUTF8codeSize);

    strDest[ nUTF8codeSize ] = 0;

    return nUTF8codeSize;

}

 

1. Formatted I/O

MACRO

ANSI

UNICODE

_tprintf

_ftprintf

_stprintf

_sntprintf

_vtprintf

_vftprintf

_vstprintf

_vsntprintf

_tscanf

_ftscanf

_stscanf

printf

fprintf

sprint

_snprintf

vprintf

vfprintf

vsprintf

_vsnprintf

scanf

fscanf

sscanf

wprintf

fwprintf

swprintf

_snwprintf

vwprintf

vfwprintf

vswprintf

_vsnwprintf

wscanf

fwscanf

swscanf

2. Formatted I/O

MACRO

ANSI

UNICODE

_fgettc

_fgetts

_fputtc

_fputts

_gettc

_gettchar

_getts

_puttc

_puttchar

_putts

_ungettc

fgetc

fgets

fputc

fputs

getc

getchar

gets

putc

putchar

puts

ungetc

fgetwc

fgetwc

fputwc

fputws

getwc

getwchar

_getws

putwc

putwchar

_putws

ungetwc

3. Stdio function

MACRO

ANSI

UNICODE

_tfopen

_tfreopen

 

_wfopen

_wfreopen

 

4. String conversion function

MACRO

ANSI

UNICODE

_tcstod

_tcstol

_tcstoul

_itot

_ltot

_ultot

_ttoi

_ttol

strtod

strtol

strtoul

_itoa

_ltoa

_ultoa

atoi

atol

wcstod

wcstol

wcstoul

_itow

_ltow

_ultow

_wtoi

_wtol

5. String function

MACRO

ANSI

UNICODE

_tcscat

_tcschr

_tcscmp

_tcscpy

_tcscspn

_tcslen

_tcsclen

_tcsncat

_tcsnccat

_tcsnccmp

_tcsncmp

_tcsncpy

_tcsnccpy

_tcspbrk

_tcsrchr

_tcsspn

_tcsstr

_tcstok

_tcsdup

_tcsicmp

_tcsncicmp

_tcsnicmp

_tcsnset

_tcsncset

_tcsrev

_tcsset

_tcslwr

_tcsupr

strcat

strchr

strcmp

strcpy

strcspn

strlen

strlen

strncat

strncat

strncmp

strncmp

strncpy

strncpy

strpbrk

strrchr

strspn

strstr

strtok

_strdup

_stricmp

_strnicmp

_strnicmp

_strnset

_strnset

_strrev

_strset

_strlwr

_strupr

wcscat

wcschr

wcscmp

wcscpy

wcscspn

wcslen

wcslen

wcsncat

wcsncat

wcsncmp

wcsncmp

wcsncpy

wcsncpy

wcspbrk

wcsrchr

wcsspn

wcsstr

wcstok

_wcsdup

_wcsicmp

_wcsnicmp

_wcsnicmp

_wcsnset

_wcsnset

_wcsrev

_wcsset

_wcslwr

_wcsupr

6. ctype function

MACRO

ANSI

UNICODE

_istalpha

_istupper

_istlower

_istdigit

_istxdigit

_istspace

_istpunct

_istalnum

_istprint

_istgraph

_istcntrl

_istascii

_totupper

_totlower

isalpha

isupper

islower

isdigit

isxdigit

isspace

ispunct

isalnum

isprint

isgraph

iscntrl

isascii

toupper

tolower

iswalpha

iswupper

iswlower

iswdigit

iswxdigit

iswspace

iswpunct

iswalnum

iswprint

iswgraph

iswcntrl

iswascii

towupper

towlower

<참고 , 펌>

http://demo.initech.com/?document_srl=4077