#include <string>
#include <stdio.h>
#include "windows.h"
// 把UTF-8转换成Unicode
void UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
// Unicode 转换成UTF-8
void UnicodeToUTF_8(char* pOut,WCHAR* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
// 把Unicode 转换成 GB2312
void UnicodeToGB2312(char* pOut,unsigned short uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
return;
}
// GB2312 转换成 Unicode
void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return;
}
//GB2312 转为 UTF-8
void GB2312ToUTF_8(CString& pOut,char *pText, int pLen)
{
char buf[1024];
char* rst = new char[pLen + (pLen >> 2) + 2];
memset(buf,0,1024);
memset(rst,0,pLen + (pLen >> 2) + 2);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
strcpy(&rst[j],"\0");
//返回结果
pOut = rst;
delete []rst;
return;
}
//UTF-8 转为 GB2312
void UTF_8ToGB2312(CString &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText[i] > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
strcpy(&newBuf[j],"\0");
pOut = newBuf;
delete []newBuf;
return;
}
CString UTF8_Encode(LPTSTR strUnicode)
{
long TLen ;
CString UTF8_EncodeLong ;
TLen = CString(strUnicode).GetLength() ;
if(TLen == 0)
{
return CString(strUnicode);
}
long lngBufferSize ;
long lngResult ;
//Set buffer for longest possible string.
lngBufferSize = TLen * 3 + 1 ;
char *bytUtf8 = new char[lngBufferSize] ;
//Translate using code page 65001(UTF-8).
lngResult = WideCharToMultiByte(CP_UTF8, 0, (unsigned short*)strUnicode, TLen, bytUtf8, lngBufferSize, NULL, 0) ;
bytUtf8[lngResult] = NULL ;
return CString(bytUtf8) ;
}
/*************************************************************************/
CString URLEncode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
BYTE b = 0;
//alloc out buffer
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
// do encoding
while (*pInTmp)
{
if(isalnum(*pInTmp))
*pOutTmp++ = *pInTmp;
else
if(isspace(*pInTmp))
*pOutTmp++ = '+';
else
// if(*pInTmp<=127)
// *pOutTmp++ = *pInTmp;
// else
{
*pOutTmp++ = '%';
*pOutTmp++ = toHex(*pInTmp>>4);
*pOutTmp++ = toHex(*pInTmp%16);
}
pInTmp++;
}
*pOutTmp = '\0';
//sOut=pOutBuf;
//delete [] pOutBuf;
sOut.ReleaseBuffer();
}
sIn.ReleaseBuffer();
return sOut;
}
使用///////////////////////////////////
CString memname=“汉字";
GB2312ToUTF_8(memname,mem_name,TCBUFSIZE);
URLEncode(memname)便是所要的结果"“%E6%B1%89%E5%AD%97"了。
一. 使用vc++6.0 调用vs2008 c++写的dll,6.0默认的编码是ANSI编码,2008默认的都是unicode编码,所以参数处理的时候会有问题。
下面是两种方法将cstring转成unicode的
方法1:
- //ANSI字符转为Unicode字符,用MultiByteToWideChar
- CString strTmp;
- CString m_ANSI = "你好!测试,hello";
- DWORD dwNum;
- dwNum = MultiByteToWideChar(CP_ACP, 0, m_ANSI, -1, NULL, 0);
- wchar_t *pwText;
- pwText = new wchar_t[dwNum];
- dwNum = MultiByteToWideChar(CP_ACP, 0, m_ANSI, -1, pwText, dwNum);
- //unicode内容在pwText中,下面显示代码
- CString m_Unicode = "";
- for(int i = 0; i < (int)dwNum - 1; i++)
- {
- strTmp.Format("%04X ", pwText[i]);
- m_Unicode += strTmp;
- }
- delete[] pwText;
方法2(我使用的:)
- CString str= "sfdkjasfdjk ";
- BSTR bstr=str.AllocSysString();
- //bstr就是UNICODE文本了!
- //用完以后要释放
- SysFreeString(bstr);
二、如果你用的是vc6.0调用vs2008,就是ansi编码的调用unicode编码的函数,返回来的是unicode的值,需要将unicode转换成ansi编码。相反可以用这个MultiByteToWideChar 函数。
- wcharText = new char[4000];
- GetPageText(i,wcharText, nTxtLen);//调用函数以后变成了wchar,就是unicode编码的
- int nBufLen = WideCharToMultiByte(936, 0, (const unsigned short *)wcharText, -1, NULL, 0, NULL, NULL);
- char* charTxt = new char[nBufLen];
- WideCharToMultiByte(936, 0, (const unsigned short *)wcharText, -1, charTxt, nBufLen, NULL, NULL);