...下用c语言调用mysql查询语句后返回的汉字显示乱码,有什么办法吗...
发布网友
发布时间:2022-04-29 02:10
我来回答
共2个回答
懂视网
时间:2022-05-07 12:16
mysqlc语言
跪求大神指导。。
热心网友
时间:2022-05-07 09:24
这是因为UTF8编码格式的问题,你需要进行字符转换。
首先用MultiByteToWideChar(CP_UTF8,0,lpszBuffer,-1,(LPWSTR)pUnicode,size); 把UTF8转成宽字节文字,
然后用WideCharToMultiByte(936,0,(LPWSTR)pUnicode,-1,lpszContext,size,NULL,NULL); 转换成CP936编码的中文汉字。
只能帮你找个思路,具体的函数调用细节我也不计啦,自己查win32api的手册吧。可以参考下面的例子。
VC6.0 UCS2(unicode) 与 GBK(gb2312) UTF(utf-8)格式互转
#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"了。