---
title: "gbk-utf-8"
author: "philip01"
date: "2009-11-10T14:36:45Z"
tags: ["C", "C#", "C++", "游戏", "编程"]
slug: "gbk-utf-8"
draft: false
lastmod: "2009-11-17T15:21:18Z"
id: 513621
url: "https://philip01.iteye.com/blog/513621"
---

gcc -o msg stdsoap2.c soapC.c soapClient.c client.c stdsoap2.c  
更多使用例子，可以查看gsoap附带的sample目录。  
编码转换的例子，保证使用utf8传输  
int GBKtoUTF8(char \*fromstr,size\_t fromlen,char \*tostr,size\_t tolen)  
{  
    int r;   
    iconv\_t cd;

    if ((cd = iconv\_open("GBK","UTF-8")) == (iconv\_t)-1) {  
        fprintf(stderr, "iconv\_open from UTF to GBK error: %s\\n", strerror(errno));  
        return -1;  
    }

    r = iconv(cd,&fromstr,&fromlen,&tostr,&tolen);  
    if (r &lt; 0) {         
        fprintf(stderr, "iconv from UTF to GBK error: %s\\n", strerror(errno));  
        iconv\_close(cd);  
        return -2;  
    }

    iconv\_close(cd);  
    return 0;  
}

 

 

#include &lt;windows.h&gt;

#include &lt;vector&gt;

#include &lt;string&gt;

using namespace std;

 

//utf8 转 Unicode

std::wstring Utf82Unicode(const std::string& utf8string)  
{  
 int widesize = ::MultiByteToWideChar(CP\_UTF8, 0, utf8string.c\_str(), -1, NULL, 0);  
 if (widesize == ERROR\_NO\_UNICODE\_TRANSLATION)  
 {  
  throw std::exception("Invalid UTF-8 sequence.");  
 }  
 if (widesize == 0)  
 {  
  throw std::exception("Error in conversion.");  
 }  
   
 std::vector&lt;wchar\_t&gt; resultstring(widesize);  
   
 int convresult = ::MultiByteToWideChar(CP\_UTF8, 0, utf8string.c\_str(), -1, &resultstring\[0], widesize);  
   
 if (convresult != widesize)  
 {  
  throw std::exception("La falla!");  
 }  
   
 return std::wstring(&resultstring\[0]);  
}

//unicode 转为 ascii

string WideByte2Acsi(wstring& wstrcode)  
{  
 int asciisize = ::WideCharToMultiByte(CP\_OEMCP, 0, wstrcode.c\_str(), -1, NULL, 0, NULL, NULL);  
 if (asciisize == ERROR\_NO\_UNICODE\_TRANSLATION)  
 {  
  throw std::exception("Invalid UTF-8 sequence.");  
 }  
 if (asciisize == 0)  
 {  
  throw std::exception("Error in conversion.");  
 }  
    std::vector&lt;char&gt; resultstring(asciisize);  
 int convresult =::WideCharToMultiByte(CP\_OEMCP, 0, wstrcode.c\_str(), -1, &resultstring\[0], asciisize, NULL, NULL);  
   
 if (convresult != asciisize)  
 {  
  throw std::exception("La falla!");  
 }  
   
    return std::string(&resultstring\[0]);  
}

 

//utf-8 转 ascii

string UTF\_82ASCII(string& strUtf8Code)  
{  
 string strRet("");

 //先把 utf8 转为 unicode  
 wstring wstr = Utf82Unicode(strUtf8Code);

 //最后把 unicode 转为 ascii  
 strRet = WideByte2Acsi(wstr);

 return strRet;  
}

///////////////////////////////////////////////////////////////////////

//ascii 转 Unicode

wstring Acsi2WideByte(string& strascii)  
{  
 int widesize = MultiByteToWideChar (CP\_ACP, 0, (char\*)strascii.c\_str(), -1, NULL, 0);  
 if (widesize == ERROR\_NO\_UNICODE\_TRANSLATION)  
 {  
  throw std::exception("Invalid UTF-8 sequence.");  
 }  
 if (widesize == 0)  
 {  
  throw std::exception("Error in conversion.");  
 }  
    std::vector&lt;wchar\_t&gt; resultstring(widesize);  
 int convresult = MultiByteToWideChar (CP\_ACP, 0, (char\*)strascii.c\_str(), -1, &resultstring\[0], widesize);

 if (convresult != widesize)  
 {  
  throw std::exception("La falla!");  
 }  
   
    return std::wstring(&resultstring\[0]);  
}

//Unicode 转 Utf8

std::string Unicode2Utf8(const std::wstring& widestring)  
{  
 int utf8size = ::WideCharToMultiByte(CP\_UTF8, 0, widestring.c\_str(), -1, NULL, 0, NULL, NULL);  
 if (utf8size == 0)  
 {  
  throw std::exception("Error in conversion.");  
 }  
   
 std::vector&lt;char&gt; resultstring(utf8size);  
   
 int convresult = ::WideCharToMultiByte(CP\_UTF8, 0, widestring.c\_str(), -1, &resultstring\[0], utf8size, NULL, NULL);  
   
 if (convresult != utf8size)  
 {  
  throw std::exception("La falla!");  
 }  
   
 return std::string(&resultstring\[0]);  
}

//ascii 转 Utf8

string ASCII2UTF\_8(string& strAsciiCode)  
{  
 string strRet("");

 //先把 ascii 转为 unicode  
 wstring wstr = Acsi2WideByte(strAsciiCode);

 //最后把 unicode 转为 utf8

 strRet = Unicode2Utf8(wstr);

 return strRet;  
}

 

[](http://blog.csdn.net/freezezdj/archive/2009/10/22/4712211.aspx)

 

在编程过程中需要对字符串进行不同的转换，特别是Gb2312和Utf-8直接的转换。在几个开源的魔兽私服中，很多都是老外开发的，而暴雪为了能够兼容世界上的各个字符集也使用了UTF-8。在中国使用VS（VS2005以上版本）开发基本都是使用Gb2312的Unicode字符集，所以当在编程过程中就需要进行字符转换，这样才能兼容游戏，否则就是乱码。而在控制台显示字符串时，真好相反需要将UTF-8的字符串转换成Gb2312才能正常显示。

为了解决这个问题，本人将其代码贴出来；其实很多地方都可以使用到字符串的编码转换，代码如下：

//UTF-8到GB2312的转换  
char* U2G(const char* utf8)  
{  
int len = MultiByteToWideChar(CP\_UTF8, 0, utf8, -1, NULL, 0);  
wchar\_t* wstr = new wchar\_t\[len+1];  
memset(wstr, 0, len+1);  
MultiByteToWideChar(CP\_UTF8, 0, utf8, -1, wstr, len);  
len = WideCharToMultiByte(CP\_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);  
char* str = new char\[len+1];  
memset(str, 0, len+1);  
WideCharToMultiByte(CP\_ACP, 0, wstr, -1, str, len, NULL, NULL);  
if(wstr) delete\[] wstr;  
return str;  
}

//GB2312到UTF-8的转换  
char* G2U(const char* gb2312)  
{  
int len = MultiByteToWideChar(CP\_ACP, 0, gb2312, -1, NULL, 0);  
wchar\_t* wstr = new wchar\_t\[len+1];  
memset(wstr, 0, len+1);  
MultiByteToWideChar(CP\_ACP, 0, gb2312, -1, wstr, len);  
len = WideCharToMultiByte(CP\_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);  
char* str = new char\[len+1];  
memset(str, 0, len+1);  
WideCharToMultiByte(CP\_UTF8, 0, wstr, -1, str, len, NULL, NULL);  
if(wstr) delete\[] wstr;  
return str;  
}