unicode转utf8
char* EncodeToUTF8(const char* mbcsStr)
{
wchar_t* wideStr;
char* utf8Str;
int charLen;
charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0);
wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen);
MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, wideStr, charLen);
charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);
utf8Str = (char*)malloc(charLen);
WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, charLen, NULL, NULL);
free(wideStr);
return utf8Str;
}
utf8转unicode
char* UTF8ToEncode(const char* mbcsStr)
{
wchar_t* wideStr;
char* unicodeStr;
int charLen;
charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0);
wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen);
MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, wideStr, charLen);
charLen = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);
unicodeStr = (char*)malloc(charLen);
WideCharToMultiByte(CP_ACP, 0, wideStr, -1, unicodeStr, charLen, NULL, NULL);
free(wideStr);
return unicodeStr;
}
写入文件时,如果是utf8格式,则
const int UTF8_TXT_FLG = 0xbfbbef;
f.Write(&UTF8_TXT_FLG , 3); //写入头部
f.Write(chp,strlen(chp)*sizeof(char));
Ansi转Unicode
void CConvertDlg::OnBnClickedButtonAnsiToUnicode()
{
// ansi to unicode
char* szAnsi = "abcd1234你我他";
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
//unicode版的MessageBox API
::MessageBoxW(GetSafeHwnd(), wszString, wszString, MB_OK);
//接下来写入文本
//写文本文件,头2个字节0xfeff,低位0xff写在前
CFile cFile;
cFile.Open(_T("1.txt"), CFile::modeWrite | CFile::modeCreate);
//文件开头
cFile.SeekToBegin();
cFile.Write("\xff\xfe", 2);
//写入内容
cFile.Write(wszString, wcsLen * sizeof(wchar_t));
cFile.Flush();
cFile.Close();
delete[] wszString;
wszString =NULL;
//方法2
//设置当前地域信息,不设置的话,使用这种方法,中文不会正确显示
//需要#include<locale.h>
setlocale(LC_CTYPE, "chs");
wchar_t wcsStr[100];
//注意下面是大写S,在unicode中,代表后面是ansi字符串
//swprintf是sprintf的unicode版本
//格式的前面要加大写L,代表是unicode
swprintf(wcsStr, L"%S", szAnsi);
::MessageBoxW(GetSafeHwnd(), wcsStr, wcsStr, MB_OK);
}
Unicode转Ansi
void CConvertDlg::OnBnClickedButtonUnicodeToAnsi()
{
// unicode to ansi
wchar_t* wszString = L"abcd1234你我他";
//预转换,得到所需空间的大小,这次用的函数和上面名字相反
int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
//同上,分配空间要给'\0'留个空间
char* szAnsi = new char[ansiLen + 1];
//转换
//unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
//最后加上'\0'
szAnsi[ansiLen] = '\0';
//Ansi版的MessageBox API
::MessageBoxA(GetSafeHwnd(), szAnsi, szAnsi, MB_OK);
//接下来写入文本
//写文本文件,ANSI文件没有BOM
CFile cFile;
cFile.Open(_T("1.txt"), CFile::modeWrite | CFile::modeCreate);
//文件开头
cFile.SeekToBegin();
//写入内容
cFile.Write(szAnsi, ansiLen * sizeof(char));
cFile.Flush();
cFile.Close();
delete[] szAnsi;
szAnsi =NULL;
//方法2
//和上面一样有另一种方法
setlocale(LC_CTYPE, "chs");
char szStr[100];
//注意下面是大写,在ansi中,代表后面是unicode字符串
//sprintf
sprintf(szStr, "%S", wszString);
::MessageBoxA(GetSafeHwnd(), szStr, szStr, MB_OK);
}
Unicode转UTF8
void CConvertDlg::OnBnClickedButtonUnicodeToU8()
{
// unicode to UTF8
wchar_t* wszString = L"abcd1234你我他";
//预转换,得到所需空间的大小,这次用的函数和上面名字相反
int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
//同上,分配空间要给'\0'留个空间
//UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存
char* szU8 = new char[u8Len + 1];
//转换
//unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL);
//最后加上'\0'
szU8[u8Len] = '\0';
//MessageBox不支持UTF8,所以只能写文件
//接下来写入文本
//写文本文件,UTF8的BOM是0xbfbbef
CFile cFile;
cFile.Open(_T("1.txt"), CFile::modeWrite | CFile::modeCreate);
//文件开头
cFile.SeekToBegin();
//写BOM,同样低位写在前
cFile.Write("\xef\xbb\xbf", 3);
//写入内容
cFile.Write(szU8, u8Len * sizeof(char));
cFile.Flush();
cFile.Close();
delete[] szU8;
szU8 =NULL;
}
UTF8转UNICODE
void CConvertDlg::OnBnClickedButtonU8ToUnicode()
{
//UTF8 to Unicode
//由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式
char* szU8 = "abcd1234\xe4\xbd\xa0\xe6\x88\x91\xe4\xbb\x96\x00";
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
//unicode版的MessageBox API
::MessageBoxW(GetSafeHwnd(), wszString, wszString, MB_OK);
//写文本同ansi to unicode
}