1. string Convert(string str, int sourceCodepage, int targetCodepage)
2. {
3. size_t len=str.length();
4. size_t unicodeLen=MultiByteToWideChar(sourceCodepage,0,str.c_str(),-1,NULL,0);
5.
6. wchar_t* pUnicode = NULL;
7. pUnicode=new wchar_t[unicodeLen+1];
8. memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
9. MultiByteToWideChar(sourceCodepage,0,str.c_str(),-1,(LPWSTR)pUnicode,(int)unicodeLen);
10.
11. BYTE* pTargetData = NULL;
12. int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,
13. (char *)pTargetData,0,NULL,NULL);
14. pTargetData=new BYTE[targetLen+1];
15. memset(pTargetData,0,targetLen+1);
16.
17. WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,
18. (char*)pTargetData,targetLen,NULL,NULL);
19. string rt((char*)pTargetData);
20.
21. delete [] pUnicode;
22. delete [] pTargetData;
23. return rt;
24. }
本文来自CSDN博客,转载请标明出处:http:http://blog.csdn.net/linuxbai/archive/2009/08/06/4418498.aspx
|