UNICODE下的字符转化问题 MultiByteToWideChar和WideCharToMultiByte两个转化函数怎么在UNICODE工程里怎么把CString字符串转化?函数给的参数搞的我头都大了,就是函数的输入和输出都要CString给出代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CString本来就是根据你工程是否unicode编码来自动生成是否支持unicdoe.//Unicode转换Ansi处理LPWSTR pwbBuffer=new WCHAR[255];PBYTE sourValue = new BYTE[255]; DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pwbBuffer,-1,NULL,0,NULL,FALSE);//计算要使用空间 dwCount=dwNum;//unicode返回的值要是转换后的大小 if(!(sourValue = (BYTE *)malloc(dwNum))) { return false; } WideCharToMultiByte (CP_OEMCP,NULL,pwbBuffer,-1,(PBYTE)sourValue,dwNum,NULL,FALSE); USES_CONVERSION;CString str = L"abc";char* p = W2A(str); CString CFileIndex::GBToUTF8(CString cstr){ CString result; WCHAR *strSrc; TCHAR *szRes; const char* str = (LPCTSTR)cstr; //获得临时变量的大小 int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); strSrc = new WCHAR[i+1]; MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i); //获得临时变量的大小 i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL); szRes = new TCHAR[i+1]; int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL); result = szRes; delete []strSrc; delete []szRes; return result;}这是我读取两种不同格式的文件的代码,把GB的换成UTF-8的 CString m_FilePath;CW2A lpFilePath(m_FilePath);//lpFilePath现在是char*TCHAR *lptstr = m_FilePath.GetBuffer();//转换成TCHAR 人家MultiByteToWideChar参数不是CString啊,你凭什么一定要“输入和输出都要CString”呢?要这样的函数,自己开发啊,否则,就得按照规则办事。 恩我的是从两文件读的数据格式不一样,不是纯粹的CString CString所储存的字符串是与项目所使用的字符集相同的,同一项目中不能即储存多字节字符串又储存Unicode字符串。在VS2005中,可以用CStringA类储存多字节字符串,用CStringW储存Unicode字符串。 我没有说是函数的参数一定要CString,只要是能转就成,但转我这里编译不过。哪位好新人能给出能编译的代码段来,环境是VS2005的UNICODE。 sffofn 朋友谢谢您给出代码,我是新手,报了一堆的错误,不知道为什么 CString cstr = _T("asdasd");const char* str = (LPCTSTR)cstr;//错误 1 error C2440: “初始化”: 无法从“LPCTSTR”转换为“const char *” d:\Works\我的网站\www.6692.com\Tools\C++\1\1Dlg.cpp 213 CString strText = _T("XXXXXXXXXX"); LPWSTR lpwText = strText.GetBuffer(strText.GetLength()); strText.ReleaseBuffer(); char buff[MAX_LENGTH]; int iSizeS = wcslen(lpwText); WideCharToMultiByte(CP_ACP, 0, lpwText, iSizeS, buff, sizeof(buff), NULL, NULL); // 将宽字节(UNICODE)转化成多字节(ANSI)的 buff[iSizeS] = '\0'; 你可以使用 MultiByteToWideChar(CP_ACP, 0, buff1, sizeof(buff1), wbuff, sizeof(wbuff)); 将多字节转化成宽字节。 有char 和wchar_t两种类型的缓冲区。 VS2005可以这样相互转换: CString strT = _T("字符串"); CStringA strA = CT2A(strT); CStringW strW = CT2W(strT); strT = CA2T(strA); strT = CW2T(strW); strA = CW2A(strW); strW = CA2W(strA); 既然定义了/ _UNICODE,那么CString的对象就是UNICODE编码,把const char * str改成 const TCHAR* str就行,或者const wchar_t* str或者 const unsigned short* str;为了方便程序在编码上的转换,最好都用tchar.h文件里面的类型。还有lz的问题,没有道理,自己去看看cstring的构造函数去,不看mfc源码,真可怕。 vc7.1和MFC做ActiveX控件,向导内各项的含义怎么理解 AMT 的SDK中LocalAgent 本地执行失败(进者有分) 求加密简单的算法 哪位兄弟会VB的帮我转个代码 成功即结贴 LPOLESTR如何转为CString MFC中的非模式对话框的相互调用 有用过CNewMenu类的吗?我用时出了问题,不知道为什么? 学了VC++6.0然后再学VC++.net,是不是又要重新开始,它们的变化大不大? 高手,可以回答简单问题吗? 如何打印位图?100分 谁 做过VISA GBIP0-网口的问题?大侠,帮帮忙啊? HP 8594E 用户编程手册(主要是发命令控制机器这一块),谁有谢谢?
LPWSTR pwbBuffer=new WCHAR[255];
PBYTE sourValue = new BYTE[255]; DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pwbBuffer,-1,NULL,0,NULL,FALSE);//计算要使用空间
dwCount=dwNum;//unicode返回的值要是转换后的大小 if(!(sourValue = (BYTE *)malloc(dwNum)))
{
return false;
} WideCharToMultiByte (CP_OEMCP,NULL,pwbBuffer,-1,(PBYTE)sourValue,dwNum,NULL,FALSE);
CString str = L"abc";
char* p = W2A(str);
CString CFileIndex::GBToUTF8(CString cstr)
{
CString result;
WCHAR *strSrc;
TCHAR *szRes;
const char* str = (LPCTSTR)cstr;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i+1];
int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szRes;
delete []strSrc;
delete []szRes;
return result;
}这是我读取两种不同格式的文件的代码,把GB的换成UTF-8的
CString m_FilePath;
CW2A lpFilePath(m_FilePath);//lpFilePath现在是char*TCHAR *lptstr = m_FilePath.GetBuffer();//转换成TCHAR
哪位好新人能给出能编译的代码段来,环境是VS2005的UNICODE。
CString cstr = _T("asdasd");
const char* str = (LPCTSTR)cstr;
//错误 1 error C2440: “初始化”: 无法从“LPCTSTR”转换为“const char *” d:\Works\我的网站\www.6692.com\Tools\C++\1\1Dlg.cpp 213
LPWSTR lpwText = strText.GetBuffer(strText.GetLength());
strText.ReleaseBuffer(); char buff[MAX_LENGTH];
int iSizeS = wcslen(lpwText);
WideCharToMultiByte(CP_ACP, 0, lpwText, iSizeS, buff, sizeof(buff), NULL, NULL); // 将宽字节(UNICODE)转化成多字节(ANSI)的
buff[iSizeS] = '\0'; 你可以使用
MultiByteToWideChar(CP_ACP, 0, buff1, sizeof(buff1), wbuff, sizeof(wbuff));
将多字节转化成宽字节。
有char 和wchar_t两种类型的缓冲区。
CString strT = _T("字符串");
CStringA strA = CT2A(strT);
CStringW strW = CT2W(strT);
strT = CA2T(strA);
strT = CW2T(strW);
strA = CW2A(strW);
strW = CA2W(strA);
为了方便程序在编码上的转换,最好都用tchar.h文件里面的类型。
还有lz的问题,没有道理,自己去看看cstring的构造函数去,不看mfc源码,真可怕。