天天看点

【转载】实现UTF8与GB2312编码格式相互转换(VC)已经验证!

utf-8编码:[1,1,1,0,a5,a6,a7,a8],[1,0,b3,b4,b5,b6,b7,b8],[1,0,c3,c4,c5,c6,c7,c8];对应的unicode编码:[a5,a6,a7,a8,b3,b4,b5,b6],[b7,b8,c3,c4,c5,c6,c7,c8]cstring

logindlg::gb2312toutf8(char

*str)

{

 cstring

result;

 wchar *strsrc = null;

 tchar

*szres = null;

 int

i;

 //gb2312转换成unicode

  i

= multibytetowidechar(cp_acp, 0, str, -1, null,

0);

  strsrc = new

wchar[i+1];

  multibytetowidechar(cp_acp, 0, str, -1,

strsrc,

i);

 //unicode转换成utf8

 i

= widechartomultibyte(cp_utf8, 0, strsrc, -1, null, 0, null,

null);

 szres = new

tchar[i+1];

 widechartomultibyte(cp_utf8, 0, strsrc, -1,

szres, i, null, null);

 result =

szres;

 if (strsrc !=

null)

 {

  delete

[]strsrc;

  strsrc =

null;

 }

 if (szres !=

[]szres;

  szres =

 return

}cstring logindlg::utf8togb2312(char

 //

utf8转换成unicode

 i = multibytetowidechar(cp_utf8, 0, str,

-1, null, 0);

 strsrc = new

 multibytetowidechar(cp_utf8, 0, str, -1,

strsrc, i);

unicode转换成gb2312

 i = widechartomultibyte(cp_acp, 0,

strsrc, -1, null, 0, null, null);

 widechartomultibyte(cp_acp, 0, strsrc, -1,

}

参考网址: