[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;int main()
{
   String* unicodeString = S"This String* contains the unicode character Pi(\u03a0)";   // Create two different encodings.
   Encoding * ascii = Encoding::ASCII;
   Encoding * unicode = Encoding::Unicode;   // Convert the String* into a Byte->Item[].
   Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);   // Perform the conversion from one encoding to the other.
   Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);   // Convert the new Byte into[] a char and[] then into a String*.
   // This is a slightly different approach to converting to illustrate
   // the use of GetCharCount/GetChars.
   Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
   ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
   String* asciiString = new String(asciiChars);   // Display the strings created before and after the conversion.
   Console::WriteLine(S"Original String*: {0}", unicodeString);
   Console::WriteLine(S"Ascii converted String*: {0}", asciiString);
}

解决方案 »

  1.   


    class URLCode
    {
    protected:
    char *cBuffer;
    wchar_t *wBuffer;
    public:
    URLCode(void);
    ~URLCode(void);
    char* Wchar_tToChar(const wchar_t *wBuf);
    wchar_t* CharToWchar_t(const char *cBuf); CString DecToHex(int dec);
    long HexToDec(CString &strHex); CString Encode(void);
    CString Encode(char *str);
    CString Encode(wchar_t *str); CString Decode(wchar_t *str);
    CString Decode(char *str);
    };
    #include "URLCode.h"URLCode::URLCode(void)
    {
    this->cBuffer=NULL;
    this->wBuffer=NULL;
    }URLCode::~URLCode(void)
    {
    delete []this->cBuffer;
    delete []this->wBuffer;
    }char* URLCode::Wchar_tToChar(const wchar_t *wBuf)
    {
    delete []this->cBuffer;
    delete []this->wBuffer; size_t wLen=wcslen(wBuf); this->wBuffer=new wchar_t[wLen+1];
    wcscpy_s(this->wBuffer,wLen+1,wBuf);
    this->wBuffer[wLen]=L'\0'; size_t cLen=WideCharToMultiByte(CP_ACP,0,wBuf,-1,NULL,0,NULL,NULL);
    this->cBuffer=new char[cLen+1];
    WideCharToMultiByte(CP_ACP,0,wBuf,-1,this->cBuffer,cLen+1,NULL,NULL);
    this->cBuffer[cLen]='\0'; return this->cBuffer;
    }wchar_t* URLCode::CharToWchar_t(const char *cBuf)
    {
    delete []this->cBuffer;
    delete []this->wBuffer; size_t cLen=strlen(cBuf); this->cBuffer=new char[cLen+1];
    strcpy_s(this->cBuffer,cLen+1,cBuf);
    this->cBuffer[cLen]='\0'; size_t wLen=MultiByteToWideChar(CP_ACP,0,cBuf,cLen+1,NULL,0);
    this->wBuffer=new wchar_t[wLen+1];
    MultiByteToWideChar(CP_ACP,0,cBuf,cLen+1,this->wBuffer,wLen);
    this->wBuffer[wLen]=L'\0'; return this->wBuffer;
    }CString URLCode::DecToHex(int dec)
    {
    wchar_t HexList[]={L"0123456789ABCDEF"}; CString Result=L"";
    while(dec>0)
    {
    Result=HexList[dec%16]+Result;
    dec/=16;
    }
    if(Result.GetLength()%2)
    {
    Result=L'0'+Result;
    } return Result;
    }long URLCode::HexToDec(CString &strHex)
    {
    wchar_t *wBuf=NULL;
    return wcstol(strHex.GetBuffer(strHex.GetLength()),&wBuf,16);
    }CString URLCode::Encode()
    {
    if(strlen(this->cBuffer)==0)
    return L"";
    return this->Encode(this->cBuffer);
    }
    CString URLCode::Encode(char *str)
    {
    CString Result=L"";
    PBYTE decByte=(PBYTE)str;
    int count=0;
    int temp=decByte[count];
    while(temp)
    {
    Result+=L"%";
    Result+=DecToHex(temp);
    count++;
    temp=decByte[count];
    }
    return Result;
    }CString URLCode::Encode(wchar_t *str)
    {
    return this->Encode(this->Wchar_tToChar(str));
    }
    CString URLCode::Decode(wchar_t *str)
    {
    CString Result=L"";
    CString Content=str;
    char *cpResult=new char[wcslen(str)*2+1];
    memset(cpResult,0,wcslen(str)*2+1);
    PBYTE pByte=(PBYTE)cpResult;
    int index=Content.Find(L"%");
    int oldIndex=0;
    if(index==-1 || index!=0)
    {
    delete []cpResult;
    return L"";
    }
    if(Content.GetLength()%3!=0)
    {
    delete []cpResult;
    return L"";
    }
    if(Content.Right(3).Left(1)!=L"%")
    {
    delete []cpResult;
    return L"";
    }
    while(index!=-1)
    {
    if(index!=0 && index!=oldIndex+3)
    {
    delete []cpResult;
    return L"";
    }
    oldIndex=index;
    if(Content.GetLength()>index+2)
    {
    int firstChar=(int)Content.Mid(index+1,1)[0];
    int secondChar=(int)Content.Mid(index+2,1)[0];
    if(((firstChar>=97 && firstChar<=102) || (firstChar>=65 && firstChar<=70) || (firstChar>=48 && firstChar<=57)) &&
    ((secondChar>=97 && secondChar<=102) || (secondChar>=65 && secondChar<=70) || (secondChar>=48 && secondChar<=57)))
    {
    long lByte=this->HexToDec(Content.Mid(index+1,2));

    *(pByte++)=(BYTE)lByte;
    index=Content.Find(L"%",index+1);

    }
    else
    {
    delete []cpResult;
    return L"";
    }
    }
    else
    {
    delete []cpResult;
    return L"";
    }
    }
    Result=this->CharToWchar_t(cpResult);
    delete []cpResult;

    return Result;
    }CString URLCode::Decode(char *str)
    {
    return this->Decode(this->CharToWchar_t(str));
    }
      

  2.   


    class TxtEncode
    {
    public:
    static const int nAnsi=0;
    static const int nUnicode=1;
    static const int nUtf8=2;
    private:
    char *ansiSorc;
    char *utf8Sorc;
    WCHAR *unicodeSorc;
    int nMode;

    size_t cLen;
    size_t wLen;
    size_t uLen; int ansiToUnicode(void);
    int unicodeToAnsi(void);
    int utf8ToUnicode(void);
    int unicodeToUtf8(void);

    public:
    TxtEncode(void);
    TxtEncode(const char *text, int nType=0);
    TxtEncode(const wchar_t *text);
    ~TxtEncode(void); char* toAnsi(void);
    WCHAR* toUnicode(void);
    char* toUtf8(void);
    int getLength(int nType);
    };#include "Windows.h"
    #include "TxtEncode.h"TxtEncode::TxtEncode(void)
    {
    this->ansiSorc=new char[1];
    this->ansiSorc[0]='\0';
    this->nMode=TxtEncode::nAnsi;
    this->cLen=0;
    }
    TxtEncode::TxtEncode(const char *text, int nType)
    {
    this->nMode=nType;
    if(this->nMode==0)
    {
    this->cLen=strlen(text);
    this->ansiSorc=new char[cLen+1];
    memset(this->ansiSorc,0,sizeof(char)*(this->cLen+1));
    sprintf_s(this->ansiSorc,cLen+1,text);
    this->ansiSorc[cLen]='\0'; this->ansiToUnicode();
    this->unicodeToUtf8();
    }
    if(this->nMode==2)
    {
    this->uLen=strlen(text);
    this->utf8Sorc=new char[this->uLen+1];
    memset(this->utf8Sorc,0,sizeof(char)*(this->uLen+1));
    sprintf_s(this->utf8Sorc,this->uLen+1,text);
    this->utf8Sorc[this->uLen]='\0'; this->utf8ToUnicode();
    this->unicodeToAnsi();
    }
    }
    TxtEncode::TxtEncode(const WCHAR *text)
    {
    this->wLen=wcslen(text);
    this->unicodeSorc=new WCHAR[this->wLen+1];
    memset(this->unicodeSorc,0,sizeof(WCHAR)*(this->wLen+1));
    swprintf_s(this->unicodeSorc,this->wLen+1,text);
    this->unicodeSorc[this->wLen]=L'\0'; this->unicodeToAnsi();
    this->unicodeToUtf8();
    }
    TxtEncode::~TxtEncode(void)
    {
    delete []this->ansiSorc;
    delete []this->unicodeSorc;
    delete []this->utf8Sorc;
    }int TxtEncode::ansiToUnicode(void)
    {
    this->wLen=MultiByteToWideChar(CP_ACP,0,this->ansiSorc,this->cLen+1,NULL,0);
    this->unicodeSorc=new WCHAR[this->wLen+1];
    MultiByteToWideChar(CP_ACP,0,this->ansiSorc,this->cLen+1,this->unicodeSorc,this->wLen);
    this->unicodeSorc[this->wLen]=L'\0'; return 0;
    }int TxtEncode::unicodeToAnsi(void)
    {
    this->cLen=WideCharToMultiByte(CP_ACP,0,this->unicodeSorc,-1,NULL,0,NULL,NULL);
    this->ansiSorc=new char[this->cLen+1];
    WideCharToMultiByte(CP_ACP,0,this->unicodeSorc,-1,this->ansiSorc,this->cLen+1,NULL,NULL);
    this->ansiSorc[this->cLen]='\0'; return 0;
    }
    int TxtEncode::utf8ToUnicode(void)
    {
    this->wLen=MultiByteToWideChar(CP_UTF8,0,this->utf8Sorc,this->uLen,NULL,0);
    this->unicodeSorc=new WCHAR[this->wLen+1];
        MultiByteToWideChar(CP_UTF8,0,this->utf8Sorc,this->uLen,this->unicodeSorc,this->wLen);
    this->unicodeSorc[this->wLen]='\0'; return 0;
    }int TxtEncode::unicodeToUtf8(void)
    {
    this->uLen=WideCharToMultiByte(CP_UTF8,0,this->unicodeSorc,-1,NULL,0,NULL,NULL);
    this->utf8Sorc=new char[this->uLen+1];
    WideCharToMultiByte(CP_UTF8,0,this->unicodeSorc,-1,this->utf8Sorc,this->uLen+1,NULL,NULL);
    this->utf8Sorc[this->uLen]='\0'; return 0;
    }char* TxtEncode::toAnsi(void)
    {
    return this->ansiSorc;
    }WCHAR* TxtEncode::toUnicode(void)
    {
    return this->unicodeSorc;
    }char* TxtEncode::toUtf8(void)
    {
    return this->utf8Sorc;
    }int TxtEncode::getLength(int nType)
    {
    if(nType==0)
    return strlen(this->ansiSorc);
    if(nType==1)
    return wcslen(this->unicodeSorc)*sizeof(WCHAR);
    if(nType==2)
    return strlen(this->utf8Sorc); return 0;
    }
      

  3.   

    原理是现成的,不过俺只写过utf8转unicode的与gb的。
      

  4.   

    CString CFileIndex::GBToUTF8(CString cstr)
    {
    CString result;
    WCHAR *strSrc;
    TCHAR *szRes;
    const char* str = (LPCTSTR)cstr;

    //获得临时变量的大小
    int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    strSrc = new WCHAR[i+1];
    MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);

    //获得临时变量的大小
    i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
    szRes = new TCHAR[i+1];
    int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL);

    result = szRes;
    delete []strSrc;
    delete []szRes;

    return result;
    }这个OK,只要你是GB转UTF-8的
      

  5.   

    上面写的,转成UTF8后的字符串显示是否正常??我也写了类似的转换代码,可是转成UTF8后,字符串显示为乱码,ANSIC和UNICODE显示都是正常的。