//把格式为sourceCodepage字符串转换为targetCodepage格式
CString CCoSMSReplies::Convert(CString str, int sourceCodepage, int targetCodepage)
{
int len = str.GetLength();
int unicodeLen = MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);
wchar_t* pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);
BYTE * pTargetData = NULL;
int targetLen = WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,
(char *)pTargetData,0,NULL,NULL);
pTargetData = new BYTE[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,
(char *)pTargetData,targetLen,NULL,NULL);
CString rt;
rt.Format("%s",pTargetData);
delete pUnicode;
pUnicode = NULL;
delete pTargetData;
pTargetData = NULL;
return rt;
}这是一个网友的代码,不知道如何将UTF-8转为GBK呢?
也就是说:int sourceCodepage, int targetCodepage 分别填多少?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4549/4549292.xml?temp=.8150446
      

  2.   

    就你的情况,分别用CP_UTF8和CP_ACP试一下。
      

  3.   

    vcmute(不争) :请问如何转?解决问题者另外开贴加200分。
      

  4.   

    Snow_Ice11111(雪上加冰) 
    用CP_UTF8和CP_ACP试过了,乱码转换后,还是乱码。vcmute(不争) 
    UTF-8先转为UNICODE,再转为GBK
    ================
    我试过的,转码后,还是乱码。不知道哪位高人有测试通过的代码?急用,有解决问题者另开贴给200分。
      

  5.   

    >UTF-8先转为UNICODE,再转为GBK
    测试没问题啊,如下
    char i,strUTF[18]={0xE4,0xB8,0x80,0xE6,0xAE,0xB5,0x55,0x54,0x46,0x2D,0x38,0xE7,0xBC,0x96,0xE7,0xA0,0x81};   
    i= MultiByteToWideChar(CP_UTF8,0,strUTF,-1,NULL,0);   
    WCHAR *strUnicode=new WCHAR[i];   
    MultiByteToWideChar (CP_UTF8,0,strUTF,-1,strUnicode,i); 

    i= WideCharToMultiByte(CP_ACP,0,strUnicode,-1,NULL,0,NULL,NULL);   
    char *strGBK = new char[i];   
    WideCharToMultiByte (CP_ACP,0,strUnicode,-1,strGBK,i,NULL,NULL);
      

  6.   

    vcmute(不争) :
    你最后提供的程序,我还没有测试,utf-8格式的字符不是这样的吗:
    %B%E%D?
      

  7.   

    vcmute(不争) :
    你的代码我测了,是正确的,我另外开一贴“vcmute(不争) 请进”,你去拿分吧。