UTF8ToGB2312

码制转换:GB2312与UTF8之间的转换

/** GB2312转UTF8,假定pcUtf8已有足够空间*/

 __inline void Gb2312ToUtf8(const char *pcGb2312, std::string & strUtf8)

 {

  //wchar_t pcUnicode[CHAR_BUFSIZE];

  //gb2312 to unicode

  Int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, pcGb2312, -1, NULL, 0);

  wchar_t * pcUnicode = new wchar_t[nUnicodeLen+1];
  memset(pcUnicode, 0, nUnicodeLen * 2 + 2);

  MultiByteToWideChar(CP_ACP, 0, pcGb2312, -1, pcUnicode, nUnicodeLen);


  //unicode to utf8

  Int nUtf8Len = WideCharToMultiByte(CP_UTF8, 0, pcUnicode, -1, NULL, 0, NULL, NULL);

  char *pcUtf8=new char[nUtf8Len + 1];
  memset(pcUtf8, 0, nUtf8Len + 1);  

  WideCharToMultiByte(CP_UTF8, 0, pcUnicode, -1, pcUtf8, nUtf8Len, NULL, NULL);

  strUtf8=pcUtf8;
  delete[] pcUtf8;
  delete[] pcUnicode;
 }


 /** UTF8转GB2312,假定pcGb2312已有足够空间*/

void CViewTree::UTF8ToGB2312(const char *pcUtf8, CString & strGB2312)

//TinyXML采用UTF_8编码转换成GB2312,这样再显示才不会出现乱码

//GB2312编码适用于汉字处理、汉字通信等系统之间的信息交换

{

//wchar_t pcUnicode[CHAR_BUFSIZE];

int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, pcUtf8, -1, NULL, 0);


wchar_t * pcUnicode = new wchar_t[nUnicodeLen+1];

memset(pcUnicode, 0, nUnicodeLen * 2 + 2);


//utf8 to unicode

MultiByteToWideChar(CP_UTF8, 0, pcUtf8, -1, pcUnicode, nUnicodeLen);


//unicode to gb2312

int nGb2312Len = WideCharToMultiByte(CP_ACP, 0, pcUnicode, -1, NULL, 0, NULL, NULL);


char *pcGb2312=new char[nGb2312Len + 1];

memset(pcGb2312, 0, nGb2312Len + 1);


WideCharToMultiByte(CP_ACP, 0, pcUnicode, -1, pcGb2312, nGb2312Len, NULL, NULL);

//WideCharToMultiByte函数映射一个unicode字符串到一个多字节字符串


strGB2312=pcGb2312;

delete[] pcGb2312;

delete[] pcUnicode;

return;

}


你可能感兴趣的:(空间)