linux下:
#define MASKBITS 0x3F
#define MASKBYTE 0x80
#define MASK2BYTES 0xC0
#define MASK3BYTES 0xE0
#define MASK4BYTES 0xF0
#define MASK5BYTES 0xF8
#define MASK6BYTES 0xFC
typedef unsigned short Unicode2Bytes;
typedef unsigned int Unicode4Bytes;
void UTF8Decode2BytesUnicode(const std::string& input, std::wstring& output)
{
output = L"";
BYTE b;
Unicode2Bytes ch;
for(size_t i=0; i < input.length();)
{
b = input;
// 1110xxxx 10xxxxxx 10xxxxxx
if((input & MASK3BYTES) == MASK3BYTES)
{
ch = ((Unicode2Bytes)(input & 0x0F) << 12) | (
(Unicode2Bytes)(input[i+1] & MASKBITS) << 6)
| (input[i+2] & MASKBITS);
i += 3;
}
// 110xxxxx 10xxxxxx
else if((input & MASK2BYTES) == MASK2BYTES)
{
ch = ((Unicode2Bytes)(input & 0x1F) << 6) | (input[i+1] & MASKBITS);
i += 2;
}
// 0xxxxxxx
else if(input < 0x80)
{
ch = input;
i += 1;
}
else
{
// assert(false);
}
output += ch;
//output.push_back(ch);
}
}
void UTF8Decode2BytesAssciChar(const std::string& input, char** output)
{
std::wstring wsStrOutput;
if (input.empty())
return;
if (*output != NULL)
{
free(*output);
*output = NULL;
}
UTF8Decode2BytesUnicode(input, wsStrOutput);
char* pChar = (char*)malloc(wsStrOutput.length() * 2 + 1);
memset(pChar, 0, wsStrOutput.length() * 2 + 1);
#ifdef WIN32
WideCharToMultiByte( CP_ACP, 0, wsStrOutput.c_str(), -1,
pChar, wsStrOutput.length() * 2 + 1, NULL, NULL );
#else
//mbstowcs() wcstombs()
assert(false);
#endif
*output = pChar;
}
//---------------------------------------
#include <iconv.h>
#include <iostream>
#define OUTLEN 255
using namespace std;
// 代碼轉換操作類
class CodeConverter {
private:
iconv_t cd;
public:
// 構造
CodeConverter(const char *from_charset,const char *to_charset) {
cd = iconv_open(to_charset,from_charset);
}
// 析構
~CodeConverter() {
iconv_close(cd);
}
// 轉換輸出
int convert(char *inbuf,int inlen,char *outbuf,int outlen) {
char **pin = &inbuf;
char **pout = &outbuf;
memset(outbuf,0,outlen);
return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen);
}
};
int main(int argc, char **argv)
{
char *in_utf8 = "姝e?ㄥ??瑁?";
char *in_gb2312 = "正在安裝";
char out[OUTLEN];
// utf-8-->gb2312
CodeConverter cc = CodeConverter("utf-8","gb2312");
cc.convert(in_utf8,strlen(in_utf8),out,OUTLEN);
cout << "utf-8-->gb2312 in=" << in_utf8 << ",out=" << out << endl;
// gb2312-->utf-8
CodeConverter cc2 = CodeConverter("gb2312","utf-8");
cc2.convert(in_gb2312,strlen(in_gb2312),out,OUTLEN);
cout << "gb2312-->utf-8 in=" << in_gb2312 << ",out=" << out << endl;
}