#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <assert.h>
char* UnicodeToUtf8(const char* unicode)
{
int len;
len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL);
char *szUtf8 = (char*)malloc(len + 1);
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL,NULL);
return szUtf8;
}
char* UnicodeToANSI(const char* strUnicode)
{
int nAnsiLen = WideCharToMultiByte(CP_ACP,0,(const wchar_t*)strUnicode,-1,NULL,0,NULL,NULL);
char *pAnsi = new char[nAnsiLen + 1];
memset((void*)pAnsi, 0, (nAnsiLen + 1) * sizeof(char));
WideCharToMultiByte(CP_ACP,0,(const wchar_t*)strUnicode,-1,pAnsi,nAnsiLen,NULL,NULL);
return pAnsi;
}
char* AnsiToUnicode(const char* str)
{
int dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
if(!dwUnicodeLen)
return strdup(str);
size_t num = dwUnicodeLen*sizeof(wchar_t);
wchar_t *pwText = (wchar_t*)malloc(num);
memset(pwText,0,num);
MultiByteToWideChar(CP_ACP,0,str,-1,pwText,dwUnicodeLen);
return (char*)pwText;
}
char* AnsiToUtf8(const char* str)
{
char* unicode = AnsiToUnicode(str);
char* utf8 = UnicodeToUtf8(unicode);
free(unicode);
return utf8;
}
char* UTF8ToUnicode(const char* str)
{
int dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
if(!dwUnicodeLen)
return strdup(str);
size_t num = dwUnicodeLen*sizeof(wchar_t);
wchar_t *pwText = (wchar_t*)malloc(num);
memset(pwText,0,num);
MultiByteToWideChar(CP_UTF8,0,str,-1,pwText,dwUnicodeLen);
return (char*)pwText;
return (char*)pwText;
}
char* Utf8ToAnsi(const char* strUtf8)
{
char* strUnicode = UTF8ToUnicode(strUtf8);
return UnicodeToANSI(strUnicode);
}