读书人

小弟我读入“”这个字再获取字符编

发布时间: 2012-01-19 00:22:28 作者: rapoo

我读入“”这个字,再获取字符编码时,发现他是GBK编码,有没有办法按照BIG5编码读入呢?
char *szBuf= "案管理出本 ";
unsigned char ch1=szBuf[1];
unsigned char ch2=szBuf[0];

CString str;
str.Format( "ch1: %d ", ch1);//szbuf[1]=163
AfxMessageBox(str);//return;

str.Format( "ch2: %d ", ch2);//szbuf[0]=140, 低字节。
AfxMessageBox(str);//return;

可见输出分别为 163和140,这不在big5编码范围之内,却在GBK之内。说明他是以GBK读入的。那么我该怎么让他读入时以big5的方式读入呢?

[解决办法]
CString 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;
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;
delete pTargetData;
return rt;

}
用法
Convert(str,936,950);

读书人网 >VC/MFC

热点推荐