Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Code Habit

c/c++ 문자열 변환 : 멀티바이트 <-> 유니코드 <-> UTF-8 본문

카테고리 없음

c/c++ 문자열 변환 : 멀티바이트 <-> 유니코드 <-> UTF-8

코드베어 2021. 6. 29. 12:32

* 유니코드 -> 멀티바이트

wchar_t strUni[256] = L"유니코드"; 
char strUtf8[256] = { 0, }; 
int nLen = WideCharToMultiByte(CP_UTF8, 0, strUni, lstrlenW(strUni), NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, strUni, lstrlenW(strUni), strUtf8, nLen, NULL, NULL);

 

* 멀티바이트 -> 유니코드

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);

 

* 유니코드 -> UTF-8

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);

 

* UTF-8 -> 유니코드

wchar_t strUnicode[256] = { 0, };
char	strUTF8[256] = { 0, };
strcpy_s(strUTF8, 256, "UTF-8글자"); // UTF-8 문자라고 가정하고..
int nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), NULL, NULL);
MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), strUnicode, nLen);

 

* 기본적으로 UTF-8로 변형할땐 유니코드 상태에서만 변형을 시켜야 한다.

 - 멀티바이트 -> 유니코드(UTF-16) -> UTF-8

 - UTF-8 -> 유니코드(UTF-16) -> UTF-8

 

 

출처 : https://icartsh.tistory.com/13