컴퓨터 공학/C++

C++ 멀티바이트 문자열을 UTF-8로 변경하는 함수

혼새미로 2020. 6. 8. 14:24
반응형
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
#include <iostream>
#include <string>
#include <Windows.h>
 
using namespace std;
 
std::string multibyte_to_utf8(const std::string& str) {
    int nLen = str.size();
    wchar_t warr[256];
    MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), -1, warr, 256);
    char carr[256];
    memset(carr, '\0'sizeof(carr));
    WideCharToMultiByte(CP_UTF8, 0, warr, -1, carr, 256NULLNULL);
    return carr;
}
 
std::string utf8_to_multibyte(const std::string& str) {
    wchar_t warr[256];
    int nLen = str.size();
    memset(warr, '\0'sizeof(warr));
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, warr, 256);
    char carr[256];
    memset(carr, '\0'sizeof(carr));
    WideCharToMultiByte(CP_ACP, 0, warr, -1, carr, 256NULLNULL);
    return carr;
}
cs
반응형