VC6.0平台下如何转换?
18 个解决方案
#1
CString CCodeTransDlg::UnicodeToJt(CString str) const
{
CString csSub;
int iLen = strlen(str) / 4;//简体中文的长度
int iRealLen=0; //对应中文的实际长度
WCHAR *uUnicode = new WCHAR[iLen];//unicode的十进制数组
for (int li=0; li<iLen; li++)
{
csSub = str.Mid(li*4, 4);
uUnicode[li] = StrHexToDec(csSub);
if (uUnicode[li]<256) iRealLen +=1;
else iRealLen +=2;
}
CString des;
WideCharToMultiByte(936, 0, uUnicode, -1, des.GetBuffer(iRealLen), iRealLen, NULL, NULL);
des.ReleaseBuffer(iRealLen);
delete[] uUnicode;
return des;
}//====简体—>DBCS码
CString CCodeTransDlg::JtToDBCS(CString str) const
{
CString csTmp,csResult="";
byte tmp;
for (int li=0; li<str.GetLength(); li++)
{
tmp = str[li];
csTmp.Format("%x", tmp);
csResult += csTmp;
}
return csResult;
}
{
CString csSub;
int iLen = strlen(str) / 4;//简体中文的长度
int iRealLen=0; //对应中文的实际长度
WCHAR *uUnicode = new WCHAR[iLen];//unicode的十进制数组
for (int li=0; li<iLen; li++)
{
csSub = str.Mid(li*4, 4);
uUnicode[li] = StrHexToDec(csSub);
if (uUnicode[li]<256) iRealLen +=1;
else iRealLen +=2;
}
CString des;
WideCharToMultiByte(936, 0, uUnicode, -1, des.GetBuffer(iRealLen), iRealLen, NULL, NULL);
des.ReleaseBuffer(iRealLen);
delete[] uUnicode;
return des;
}//====简体—>DBCS码
CString CCodeTransDlg::JtToDBCS(CString str) const
{
CString csTmp,csResult="";
byte tmp;
for (int li=0; li<str.GetLength(); li++)
{
tmp = str[li];
csTmp.Format("%x", tmp);
csResult += csTmp;
}
return csResult;
}
#2
Windows上没必要转成GBK,UTF-8还更好~~
#3
没必要转, utf-8支持中文的.
#4
我是想将数据从网上取过来放到VC6.0的程序里
网页数据是UTF-8的,将取出来的数据字串显示到VC6.0程序里
如果不转的话不知会不会有什么问题?
网页数据是UTF-8的,将取出来的数据字串显示到VC6.0程序里
如果不转的话不知会不会有什么问题?
#5
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
char szTemp[8096] = {NULL};
WideCharToMultiByte(CP_UTF8, 0, strTitle.GetBuffer(0), -1, szTemp, 8095, NULL, NULL );
#6
楼上的:
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
为什么会是转换成UTF-8呢,网页上的格式本来就是UTF-8格式的,那我取出来应该是转换成VC上能显示的才对啊。
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
为什么会是转换成UTF-8呢,网页上的格式本来就是UTF-8格式的,那我取出来应该是转换成VC上能显示的才对啊。
#7
我的意思你取出的是对, 是utf-8, 就是说你的vc用时, 看看能不能直接用. 如果不能转成vc能用的, 比如unicode的utf-18等等.
#8
int UTF8ToGB(const char* str,char *out)
{
WCHAR *strSrc;
TCHAR *szRes;
int len;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i+1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
len = (i+1)*sizeof(CHAR);
memcpy(out,szRes,len);
out[len+1] ='\0';
delete []strSrc;
delete []szRes;
return len;
}
#9
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
很麻烦啊?
很麻烦啊?
#10
还有8楼的怎么调用了MultiByteToWideChar后
又调用了WideCharToMultiByte?
好晕啊?
又调用了WideCharToMultiByte?
好晕啊?
#11
第一次调用是为了得到 足够的长度来容纳转换后的字符串. 是为了得到长度.
第二次才是真正的转换.
lz应该仔细看看8楼的代码呵呵.
#12
再问一下
如果用这两个函数的话
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
char *都没法直接赋值,CString也无法给char 赋值,晕死
很麻烦啊?
如果用这两个函数的话
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
char *都没法直接赋值,CString也无法给char 赋值,晕死
很麻烦啊?
#13
可以把unicode转成ANSI啊
#14
厄
我的是VC6.0开发平台程序,没有转成UNICODE的,但是调用
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
提示错误:
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
所以是不是必须转成UNICODE才可以用这两个函数
我的是VC6.0开发平台程序,没有转成UNICODE的,但是调用
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
提示错误:
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
所以是不是必须转成UNICODE才可以用这两个函数
#15
void XmlDeal::UTF8ToUnicodeA(char* StrChar, const CString CStr)
{
char* pTargetData = NULL;
int targetLen=WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
pTargetData=new char[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,targetLen,NULL,NULL);
//sprintf_s(StrChar, CharLen, "%s", pTargetData);
memcpy(StrChar,pTargetData,targetLen+1);
delete []pTargetData;
}
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
{
char* pTargetData = NULL;
int targetLen=WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
pTargetData=new char[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,targetLen,NULL,NULL);
//sprintf_s(StrChar, CharLen, "%s", pTargetData);
memcpy(StrChar,pTargetData,targetLen+1);
delete []pTargetData;
}
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
#16
问题解决,当然这个只是VC下的,不通用,
/*
*GBK格式转Utf8格式*
*@param strGBK: 待转换的GBK CString
*转换完成后,为Utf8 CString
*/
void XmlDeal::ConvertGBKToUtf8(CString& strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
/*
*Utf8格式转GBK格式*
*@param strGBK: 待转换的Utf8 CString
*转换完成后,为GBK CString
*/
void XmlDeal::ConvertUtf8ToGBK(CString& strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
/*
*GBK格式转Utf8格式*
*@param strGBK: 待转换的GBK CString
*转换完成后,为Utf8 CString
*/
void XmlDeal::ConvertGBKToUtf8(CString& strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
/*
*Utf8格式转GBK格式*
*@param strGBK: 待转换的Utf8 CString
*转换完成后,为GBK CString
*/
void XmlDeal::ConvertUtf8ToGBK(CString& strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
#17
http://www.codeproject.com/KB/stl/utf8facet.aspx
#18
顶http://www.codeproject.com/KB/stl/utf8facet.aspx
#1
CString CCodeTransDlg::UnicodeToJt(CString str) const
{
CString csSub;
int iLen = strlen(str) / 4;//简体中文的长度
int iRealLen=0; //对应中文的实际长度
WCHAR *uUnicode = new WCHAR[iLen];//unicode的十进制数组
for (int li=0; li<iLen; li++)
{
csSub = str.Mid(li*4, 4);
uUnicode[li] = StrHexToDec(csSub);
if (uUnicode[li]<256) iRealLen +=1;
else iRealLen +=2;
}
CString des;
WideCharToMultiByte(936, 0, uUnicode, -1, des.GetBuffer(iRealLen), iRealLen, NULL, NULL);
des.ReleaseBuffer(iRealLen);
delete[] uUnicode;
return des;
}//====简体—>DBCS码
CString CCodeTransDlg::JtToDBCS(CString str) const
{
CString csTmp,csResult="";
byte tmp;
for (int li=0; li<str.GetLength(); li++)
{
tmp = str[li];
csTmp.Format("%x", tmp);
csResult += csTmp;
}
return csResult;
}
{
CString csSub;
int iLen = strlen(str) / 4;//简体中文的长度
int iRealLen=0; //对应中文的实际长度
WCHAR *uUnicode = new WCHAR[iLen];//unicode的十进制数组
for (int li=0; li<iLen; li++)
{
csSub = str.Mid(li*4, 4);
uUnicode[li] = StrHexToDec(csSub);
if (uUnicode[li]<256) iRealLen +=1;
else iRealLen +=2;
}
CString des;
WideCharToMultiByte(936, 0, uUnicode, -1, des.GetBuffer(iRealLen), iRealLen, NULL, NULL);
des.ReleaseBuffer(iRealLen);
delete[] uUnicode;
return des;
}//====简体—>DBCS码
CString CCodeTransDlg::JtToDBCS(CString str) const
{
CString csTmp,csResult="";
byte tmp;
for (int li=0; li<str.GetLength(); li++)
{
tmp = str[li];
csTmp.Format("%x", tmp);
csResult += csTmp;
}
return csResult;
}
#2
Windows上没必要转成GBK,UTF-8还更好~~
#3
没必要转, utf-8支持中文的.
#4
我是想将数据从网上取过来放到VC6.0的程序里
网页数据是UTF-8的,将取出来的数据字串显示到VC6.0程序里
如果不转的话不知会不会有什么问题?
网页数据是UTF-8的,将取出来的数据字串显示到VC6.0程序里
如果不转的话不知会不会有什么问题?
#5
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
char szTemp[8096] = {NULL};
WideCharToMultiByte(CP_UTF8, 0, strTitle.GetBuffer(0), -1, szTemp, 8095, NULL, NULL );
#6
楼上的:
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
为什么会是转换成UTF-8呢,网页上的格式本来就是UTF-8格式的,那我取出来应该是转换成VC上能显示的才对啊。
把你的vc6程序换成unicode, 然后网上数据 应用时 用WideCharToMultiChar转化成UTF-8, 因为vc用的unicode我猜测是utf-16, 还是转换好.
为什么会是转换成UTF-8呢,网页上的格式本来就是UTF-8格式的,那我取出来应该是转换成VC上能显示的才对啊。
#7
我的意思你取出的是对, 是utf-8, 就是说你的vc用时, 看看能不能直接用. 如果不能转成vc能用的, 比如unicode的utf-18等等.
#8
int UTF8ToGB(const char* str,char *out)
{
WCHAR *strSrc;
TCHAR *szRes;
int len;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i+1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
len = (i+1)*sizeof(CHAR);
memcpy(out,szRes,len);
out[len+1] ='\0';
delete []strSrc;
delete []szRes;
return len;
}
#9
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
很麻烦啊?
很麻烦啊?
#10
还有8楼的怎么调用了MultiByteToWideChar后
又调用了WideCharToMultiByte?
好晕啊?
又调用了WideCharToMultiByte?
好晕啊?
#11
第一次调用是为了得到 足够的长度来容纳转换后的字符串. 是为了得到长度.
第二次才是真正的转换.
lz应该仔细看看8楼的代码呵呵.
#12
再问一下
如果用这两个函数的话
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
char *都没法直接赋值,CString也无法给char 赋值,晕死
很麻烦啊?
如果用这两个函数的话
不转成UNICODE不行吗?VC6.0下转成UNICODE,很多都要改,
char *都没法直接赋值,CString也无法给char 赋值,晕死
很麻烦啊?
#13
可以把unicode转成ANSI啊
#14
厄
我的是VC6.0开发平台程序,没有转成UNICODE的,但是调用
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
提示错误:
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
所以是不是必须转成UNICODE才可以用这两个函数
我的是VC6.0开发平台程序,没有转成UNICODE的,但是调用
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
提示错误:
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
所以是不是必须转成UNICODE才可以用这两个函数
#15
void XmlDeal::UTF8ToUnicodeA(char* StrChar, const CString CStr)
{
char* pTargetData = NULL;
int targetLen=WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
pTargetData=new char[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,targetLen,NULL,NULL);
//sprintf_s(StrChar, CharLen, "%s", pTargetData);
memcpy(StrChar,pTargetData,targetLen+1);
delete []pTargetData;
}
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
{
char* pTargetData = NULL;
int targetLen=WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,0,NULL,NULL);
pTargetData=new char[targetLen+1];
memset(pTargetData,0,targetLen+1);
WideCharToMultiByte(CP_UTF8,0,(LPCTSTR)CStr,-1,pTargetData,targetLen,NULL,NULL);
//sprintf_s(StrChar, CharLen, "%s", pTargetData);
memcpy(StrChar,pTargetData,targetLen+1);
delete []pTargetData;
}
error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
#16
问题解决,当然这个只是VC下的,不通用,
/*
*GBK格式转Utf8格式*
*@param strGBK: 待转换的GBK CString
*转换完成后,为Utf8 CString
*/
void XmlDeal::ConvertGBKToUtf8(CString& strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
/*
*Utf8格式转GBK格式*
*@param strGBK: 待转换的Utf8 CString
*转换完成后,为GBK CString
*/
void XmlDeal::ConvertUtf8ToGBK(CString& strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
/*
*GBK格式转Utf8格式*
*@param strGBK: 待转换的GBK CString
*转换完成后,为Utf8 CString
*/
void XmlDeal::ConvertGBKToUtf8(CString& strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
}
/*
*Utf8格式转GBK格式*
*@param strGBK: 待转换的Utf8 CString
*转换完成后,为GBK CString
*/
void XmlDeal::ConvertUtf8ToGBK(CString& strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
#17
http://www.codeproject.com/KB/stl/utf8facet.aspx
#18
顶http://www.codeproject.com/KB/stl/utf8facet.aspx