CString UTF2Cstring(char* szUtf8, int buffLen)
{
BSTR bstrWide;
char* pszAnsi;
int nLength;
// wide char 크기구하기
nLength = MultiByteToWideChar(CP_UTF8, 0, szUtf8, buffLen, NULL, NULL);
bstrWide = SysAllocStringLen(NULL, nLength);
// UTF-8 -> Unicode (UTF-16)
MultiByteToWideChar(CP_UTF8, 0, szUtf8, (int)strlen(szUtf8) + 1, bstrWide, nLength);
// multi byte 크기구하기
nLength = WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, NULL, 0, NULL, NULL);
pszAnsi = new char[nLength];
// Unicode -> multi byte
WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL);
SysFreeString(bstrWide);
// 결과string
CString tempString;
tempString = (LPSTR)pszAnsi;
// 메모리해제
delete[] pszAnsi;
return tempString;
}
#include <locale>
int Cstring2UTF(CString* cStr, char* szUtf8)
{
int length = cStr->GetLength();
wchar_t* wStr = cStr->GetBuffer();
char* mb = toMB(wStr);
int nLength = MultiByteToWideChar(CP_ACP, 0, mb, (int)strlen(mb), NULL, NULL);
BSTR bstrWide;
bstrWide = SysAllocStringLen(NULL, nLength);
MultiByteToWideChar(CP_ACP, 0, mb, (int)strlen(mb), bstrWide, nLength);
nLength = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL) - 1;
szUtf8 = (char*)realloc(szUtf8, sizeof(char)*nLength);
WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, szUtf8, nLength, NULL, NULL);
SysFreeString(bstrWide);
return nLength;
}
char* toMB(wchar_t *str_param) {
size_t convertedChars = 0;
size_t str_param_len = (wcslen(str_param) * 2) + 1;
char *mb = new char[str_param_len];
setlocale(LC_ALL, "");
wcstombs_s(&convertedChars, mb, str_param_len, str_param, str_param_len);
return mb;
}