使用场合:
VC下使用unicode编码,如果需要使用utf-8,这时就需要转换(支持中文)
//参数:utf8 要转换的utf8 指针
unicode接收转换后的buff
nBuffSize buff的大小
返回值:转换后的unicode大小
int Utf82Unicode(const char* utf, wchar_t* unicode, int nBuffSize)
{
if(!utf || !strlen(utf))
{
return 0;
}
int dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,utf,-1,NULL,0);
size_t num = dwUnicodeLen*sizeof(wchar_t);
if (num > nBuffSize)
{
return 0;
}
MultiByteToWideChar(CP_UTF8, 0, utf, -1, unicode, dwUnicodeLen);
return dwUnicodeLen;
}
//参数:unicode 要转换的unicode指针
utf8 接收转换后的buff
nBuffSize buff的大小
返回值:转换utf8大小
int Unicode2Utf8(const wchar_t* unicode, char* utf8, int nBuffSize)
{
if (!unicode || !wcslen(unicode))
{
return 0;
}
int len;
len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
if (len > nBuffSize)
{
return 0;
}
WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL,NULL);
return len;
}