Gbk互相轉換UTF8


 1 /**********************************************************************
 2 * 函數名稱:    GbkToUtf8
 3 * 功能描述:  Gbk轉化為UTF8
 4 * 輸入參數:  需要轉化的字符串(Gbk)
 5 * 輸出參數:  轉化后的字符串(UTF-8)
 6 * 返 回 值:  無
 7 * 其它說明:  
 8 * 修改日期 版本號 修改人 修改內容
 9 * -----------------------------------------------
10 * 
11 ***********************************************************************/
12 string GbkToUtf8(const std::string &strGBK)
13 {
14     string strOutUTF8 = "";
15     WCHAR *str1;
16     int n = MultiByteToWideChar(CP_ACP,0,strGBK.c_str(),-1,NULL,0);
17     str1 = new WCHAR[n];
18     MultiByteToWideChar(CP_ACP,0,strGBK.c_str(),-1,str1,n);
19     n = WideCharToMultiByte(CP_UTF8,0,str1,-1,NULL,0,NULL,NULL);
20     char *str2 = new char[n];
21     WideCharToMultiByte(CP_UTF8,0,str1,-1,str2,n,NULL,NULL);
22     strOutUTF8 = str2;
23     delete[]str1;
24     str1 = NULL;
25     delete[]str2;
26     str2 = NULL;
27     return strOutUTF8;
28 }
29 
30 /**********************************************************************
31 * 函數名稱:    Utf8ToGbk
32 * 功能描述:  UTF8轉化為Gbk
33 * 輸入參數:  需要轉化的字符串(utf8)
34 * 輸出參數:  轉化后的字符串(Gbk)
35 * 返 回 值:  無
36 * 其它說明:  
37 * 修改日期 版本號 修改人 修改內容
38 * -----------------------------------------------
39 * 
40 ***********************************************************************/
41 string Utf8ToGbk(const std::string & strUTF8)
42 {
43         string strOutGBK = "";
44         WCHAR *str1;
45         int n = MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,NULL,0);
46         str1 = new WCHAR[n];
47         MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, str1, n);
48         n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL);
49         char *str2 = new char[n];
50         WideCharToMultiByte(CP_ACP, 0, str1, -1, str2, n, NULL, NULL);
51         strOutGBK = str2;
52         delete[]str1;
53         str1 = NULL;
54         delete[]str2;
55         str2 = NULL;
56         return strOutGBK;
57 }

 

更多內容請訪問 www.uusystem.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM