CString转换成const char*的一种有效方法
2012-04-04 15:11
274 查看
在Visual Studio 200X下,CString直接转换成const char* 有点困难,下面是自己用的一种可行方案:
//得到目录路径
CString mPath=dlg.GetPathName();
//将目录路径转换成绝对路径以便cvLoadImage调用
int m=mPath.GetLength();
for(int index=0;index<m;index++)
{
if(mPath.GetAt(index)=='\\')
{
mPath.Insert(index,'\\');
index++;
}
}
//将CString转换成LPCTSTR
LPCTSTR p=mPath.GetBuffer();
/*
将LPCTSTR转换为const char *,因为Unicode的问题,LPCTSTR1、在非UNICODE环境下为 const char * 2、在UNICODE环境下为
const unsigned short *
在情况2时需要借助API函数WideCharToMultiByte
*/
DWORD dwMinSize;
dwMinSize=WideCharToMultiByte(CP_ACP,NULL,p,-1,NULL,0,NULL,FALSE);
char * pp=new char[dwMinSize];
WideCharToMultiByte(CP_OEMCP,NULL,p,-1,pp,dwMinSize,NULL,FALSE);
//读取图像
IplImage* ip1=cvLoadImage(pp,-1);
//得到目录路径
CString mPath=dlg.GetPathName();
//将目录路径转换成绝对路径以便cvLoadImage调用
int m=mPath.GetLength();
for(int index=0;index<m;index++)
{
if(mPath.GetAt(index)=='\\')
{
mPath.Insert(index,'\\');
index++;
}
}
//将CString转换成LPCTSTR
LPCTSTR p=mPath.GetBuffer();
/*
将LPCTSTR转换为const char *,因为Unicode的问题,LPCTSTR1、在非UNICODE环境下为 const char * 2、在UNICODE环境下为
const unsigned short *
在情况2时需要借助API函数WideCharToMultiByte
int WideCharToMultiByte( UINT CodePage, // code page DWORD dwFlags, // performance and mapping flags LPCWSTR lpWideCharStr, // wide-character string int cchWideChar, // number of chars in string LPSTR lpMultiByteStr, // buffer for new string int cbMultiByte, // size of buffer LPCSTR lpDefaultChar, // default for unmappable chars LPBOOL lpUsedDefaultChar // set when default char used );
*/
DWORD dwMinSize;
dwMinSize=WideCharToMultiByte(CP_ACP,NULL,p,-1,NULL,0,NULL,FALSE);
char * pp=new char[dwMinSize];
WideCharToMultiByte(CP_OEMCP,NULL,p,-1,pp,dwMinSize,NULL,FALSE);
//读取图像
IplImage* ip1=cvLoadImage(pp,-1);
相关文章推荐
- CString转换成const char*的一种有效方法
- VC2008中将CString转换成const char*的一种有效方法
- CString转换成const char*的方法
- Unicode下CString转成const char*的一种简单方法
- CString, const char*,string与Unicode,multi-byte字符集转换
- VS2010中将CString转换为constchar*
- CString和string和CONST CHAR*的转换
- 不能将参数1从“CString”转换为“const char *”
- error C2664: “fopen”: 不能将参数 1 从“CString”转换为“const char *”
- Cstring转int型问题(error C2664: “atoi”: 不能将参数 1 从“CString”转换为“const char *”))
- 将Cstring 转换成 const char *
- CString与const char*的相互转换
- VS2005中const char *转换为LPCWSTR的方法Unicode
- CString类型转换为const char*类型
- error C2440: “初始化”: 无法从“const char *”转换为“ATL::CStringT<BaseType,StringTraits>”
- 移植性问题のerror C2664: strcpy : 不能将参数 2 从 CString 转换为 const char *怎么回事?
- 不能将参数 1 从“CString”转换为“const char *
- CString转换成const char*
- UNICODE下宽字符的CString转换为const char *
- CString转换为const char*