unicode转换

193 阅读3分钟

    static std::wstring MBytesToWString(const char* lpcszString);
static std::string WStringToMBytes(const wchar_t* lpwcszWString);
static std::wstring UTF8ToWString(const char* lpcszString);
static std::string WStringToUTF8(const wchar_t* lpwcszWString);

std::wstring KKLogObject::MBytesToWString(const char* lpcszString)
{
int len = strlen(lpcszString);
int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, NULL, 0);
wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
wstring wString = (wchar_t*)pUnicode;
delete [] pUnicode;
return wString;
}

std::string KKLogObject::WStringToMBytes(const wchar_t* lpwcszWString)
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);
std::string strReturn(pElementText);
delete [] pElementText;
return strReturn;
}

std::wstring KKLogObject::UTF8ToWString(const char* lpcszString)
{
int len = strlen(lpcszString);
int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, NULL, 0);
wchar_t* pUnicode;
pUnicode = new wchar_t[unicodeLen + 1];
memset((void*)pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
wstring wstrReturn(pUnicode);
delete [] pUnicode;
return wstrReturn;
}

std::string KKLogObject::WStringToUTF8(const wchar_t* lpwcszWString)
{
char* pElementText;
int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, NULL, 0, NULL, NULL);
pElementText = new char[iTextLen + 1];
memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);
std::string strReturn(pElementText);
delete [] pElementText;
return strReturn;
}

#include <iostream>  
#include <string>  
#include <Windows.h>  
using namespace std;  

//gbk转UTF-8  
string GbkToUtf8(const std::string& strGbk)//传入的strGbk是GBK编码  
{  
//gbk转unicode  
int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);  
wchar_t *strUnicode = new wchar_t[len];  
wmemset(strUnicode, 0, len);  
MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);  

//unicode转UTF-8  
len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL);  
char * strUtf8 = new char[len];  
WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, strUtf8, len, NULL, NULL);  

std::string strTemp(strUtf8);//此时的strTemp是UTF-8编码  
delete[]strUnicode;  
delete[]strUtf8;  
strUnicode = NULL;  
strUtf8 = NULL;  
return strTemp;  
}  

//UTF-8转gbk  
string Utf8ToGbk(const std::string& strUtf8)//传入的strUtf8是UTF-8编码  
{  
//UTF-8转unicode  
int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);  
wchar_t * strUnicode = new wchar_t[len];//len = 2  
wmemset(strUnicode, 0, len);  
MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, strUnicode, len);  

//unicode转gbk  
len = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);  
char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0  
memset(strGbk, 0, len);  
WideCharToMultiByte(CP_ACP,0, strUnicode, -1, strGbk, len, NULL, NULL);  

std::string strTemp(strGbk);//此时的strTemp是GBK编码  
delete[]strUnicode;  
delete[]strGbk;  
strUnicode = NULL;  
strGbk = NULL;  
return strTemp;  
}  

//gbk转unicode (下面的例子没用到)  
wstring GbkToUnicode(const std::string& strGbk)//返回值是wstring  
{  
int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);  
wchar_t *strUnicode = new wchar_t[len];  
wmemset(strUnicode, 0, len);  
MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);  

std::wstring strTemp(strUnicode);//此时的strTemp是Unicode编码  
delete[]strUnicode;  
strUnicode = NULL;  
return strTemp;  
}  

//Unicode转gbk  
string UnicodeToGbk (const std::wstring& strUnicode)//参数是wstring  
{  
int len = WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);  
char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0  
memset(strGbk, 0, len);  
WideCharToMultiByte(CP_ACP,0,strUnicode.c_str(), -1, strGbk, len, NULL, NULL);  

std::string strTemp(strGbk);//此时的strTemp是GBK编码  
delete[]strGbk;  
strGbk = NULL;  
return strTemp;  
}  

int main()  
{  
//1、ANSI/GBK编码  
string strGbk = "我";  
int num = strGbk.size();//获取两个字符数,也是我字所占的字节数  

unsigned char* p = (unsigned char*)strGbk.c_str();      
for (int i = 0; i < num; i++)      
{      
printf("%0x", *p);      
p++;      
}  //输出ced2 所以我的GBK编码是0xced2  
printf("\n");     

char gbk[] = {0xce, 0xd2, 0x00}; //加上0x00字符串结束符,不会输出乱码  
cout<<gbk<<endl;//输出汉字我  


//2、unicodde编码  

//方法一  
//wchar_t str = 0x6211;    
//wcout.imbue(locale("chs"));   
//wcout << str << endl;//输出汉字我  

//wchar_t c=L'我';  
//cout << hex << (short)c << endl<<endl;//输出unicodde编码 6211  

//方法二:  
wstring strUnicode = L"我";//转成unicode编码  
num = strUnicode.size()*2;//乘以2,才是我所占的字节数  
p = (unsigned char*)strUnicode.c_str();      
for (int i = 0; i < num; i++)      
{      
printf("%0x", *p);      
p++;      
}  //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211  
printf("\n");     

wchar_t s[2] = {0x6211, 0x00}; //加上0x00字符串结束符,不会输出乱码  
wstring str =(wchar_t*)s;  
cout<<UnicodeToGbk(str)<<endl;//需要先将unicode字符串转成gbk之后才能用cout输出  


//3、UTF-8编码  
string strUtf8  = GbkToUtf8("我");//转成utf8编码  
num = strUtf8.size();//num=3  
p = (unsigned char*)strUtf8.c_str();      
for (int i = 0; i < num; i++)      
{      
printf("%0x", *p);      
p++;      
}  //输出e68891  
printf("\n");     

char utf8[] = {0xe6, 0x88, 0x91,0x00}; //加上0x00字符串结束符,不会输出乱码  
cout<<Utf8ToGbk(utf8)<<endl;//需要先将utf8字符串转成gbk之后才能用cout输出  


return 0;  
}