* 유니코드 -> 멀티바이트
|
1
2
3
4
|
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);
|
cs |
* 멀티바이트 -> 유니코드
|
1
2
3
4
5
|
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);
|
cs |
* 유니코드 -> UTF-8
|
1
2
3
4
5
|
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);
|
cs |
* UTF-8 -> 유니코드
|
1
2
3
4
5
|
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);
|
cs |
사용 예)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
wchar_t *mp_string;
int m_length;
void AsciiToUnicode(char *ap_str)
{
wchar_t strUnicode[256] = { 0, };
char strUTF8[256] = { 0, };
strcpy_s(strUTF8, 256, ap_str);
int nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), NULL, NULL);
MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), strUnicode, nLen);
m_length = wcslen(strUnicode);
mp_string = new wchar_t[m_length + 1];
memcpy(mp_string, strUnicode, (m_length + 1) << 1); // "<< 1"하는 방식은 "* 2"와 같은 표현 방식이다.
}
|
cs |
참고)
UTF-8로 변형할땐 유니코드 상태에서만 변형을 시켜야 한다.
- 멀티바이트 -> 유니코드(UTF-16) -> UTF-8
- UTF-8 -> 유니코드(UTF-16) -> UTF-8
'C++ > C++ 코드 기록' 카테고리의 다른 글
| 디렉터리에 파일 나열 (0) | 2022.11.04 |
|---|---|
| 레지스트리 값 구하는 방법 (0) | 2022.11.04 |
| Window OS 버전 번호 (0) | 2022.10.18 |
| shlwapi 의 파일 경로 관련 API 모음 (0) | 2022.07.12 |
| c++ UTF-8 인코딩 (0) | 2022.01.24 |