一:
代码
1Ansi_1252_ToUnicode
//Ansi_1252_ToUnicode
void Ansi_1252_ToUnicode_By936()
{
char* szAnsi = "Äã"; //szAnsi=Äã
int wcsLen = MultiByteToWideChar(936, NULL, szAnsi, strlen(szAnsi), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
MultiByteToWideChar(936, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen);
wszString[wcsLen] = '\0'; //wszString = 0x002f54d0 L"你"
}
void Ansi_1252_ToUnicode_swprintf()
{
char* szAnsi = "Äã"; //szAnsi = 0x010d8884 "Äã"
wchar_t wcsStr[100];
//注意下面是大写S,在unicode中,代表后面是ansi字符串
//swprintf是sprintf的unicode版本
//格式的前面要加大写L,代表是unicode
swprintf(wcsStr,100, L"%S", szAnsi); //wcsStr = 0x001df9cc L"你"
}
2UnicodeToAnsi
void UnicodeToAnsi_sprintf_s()
{
wchar_t* wszString = L"你";
char szStr[100];
//注意下面是大写,在ansi中,代表后面是unicode字符串
//sprintf
sprintf_s(szStr, "%S", wszString); //szStr = 0x0031fd3c "Äã"
}
void UnicodeToAnsi_By936()
{
wchar_t wszStringSource[] = L"你";
int ansiLen = WideCharToMultiByte(936, NULL, wszStringSource, wcslen(wszStringSource), NULL, 0, NULL, NULL);
char* szAnsi = new char[ansiLen + 1];
WideCharToMultiByte(936, NULL, wszStringSource, wcslen(wszStringSource), szAnsi, ansiLen, NULL, NULL);
szAnsi[ansiLen] = '\0'; //szAnsi = 0x00547bf8 "Äã"
}
void Unicode_1252_ToAnsi_By1252()
{
wchar_t* wszString = L"Äã";//wszString = 0x010988a8 L"Äã"
int ansiLen = WideCharToMultiByte(1252, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
char* szAnsi = new char[ansiLen + 1];
WideCharToMultiByte(1252, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
szAnsi[ansiLen] = '\0'; //szAnsi = 0x00085548 "Äã"
}
void Unicode_1252_ToAnsi_sprintf_s()
{
wchar_t* wszString = L"Äã";
char szStr[100];
//注意下面是大写,在ansi中,代表后面是unicode字符串
//sprintf
sprintf_s(szStr, "%S", wszString); //szStr = 0x0020f698 "Äã"
}
3utf8ToUnicode
void utf8ToUnicode_ByCP_UTF8()
{
char szU8[] = "ä½ æˆ‘ä»–";
int wcsLen = MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
wszString[wcsLen] = '\0'; //wszString = 0x003e5518 L"你我他"
}
void U8ToUnicode()
{
char* szU8 = "abcd1234\xe4\xbd\xa0\xe6\x88\x91\xe4\xbb\x96\x00"; //和utf8ToUnicode_ByCP_UTF8()一样
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
wszString[wcsLen] = '\0'; //wszString = 0x0040a5f8 L"abcd1234你我他"
}
4unicode to utf8
void UnicodeToU8_ByCP_UTF8()
{
// unicode to UTF8
wchar_t* wszString = L"你我他";
int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL);
szU8[u8Len] = '\0'; //szU8 = 0x000b5548 "abcd1234ä½ æˆ‘ä»–"
}
5
//lBuffer = 0x0027e9ac L"Äã"
int u8Len = WideCharToMultiByte(CP_UTF8, NULL, lBuffer, wcslen(lBuffer), NULL, 0, NULL, NULL);
char* szU8 = new char[u8Len + 1];
WideCharToMultiByte(CP_UTF8, NULL, lBuffer, wcslen(lBuffer), szU8, u8Len, NULL, NULL);
szU8[u8Len] = '\0'; //szU8 = 0x001c7958 "Äã"
int wcsLen = MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszStringDes = new wchar_t[wcsLen + 1];
MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszStringDes, wcsLen);
wszStringDes[wcsLen] = '\0'; //wszStringDes = 0x001c5458 L"Äã"
6
GetPrivateProfileStringA将文件block.ini内容为:Äã读入成char
//szAnsi = 0x00257c78 "Äã"