天天看點

【轉載】實作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,

}

參考網址: