MFC/WTL字符串转换(CString - char)

CString:

当VS编码环境为Unicode时,CString就是CStringW。当VS编码环境为多字节时,CString就是CStringA。

在C/C++中同样存在着两种环境的字符串:char 和 wchar_t。

本文的目的是:转换他们。

Code Start :

class UStrToAscChar		//CStringW + wchar_t* -> char*
{
public:
	UStrToAscChar(const CStringW cs)
	{
		int n = WideCharToMultiByte(CP_ACP,0,cs,-1,NULL,0,NULL,NULL);
		if(n <= 0)
			buff = NULL;
		buff = new char[n];
		memset(buff,0,n);
		WideCharToMultiByte(CP_ACP,0,cs,-1,buff,n,NULL,NULL);
	}
	~UStrToAscChar()
	{
		delete buff;
	}
	const char* operator &()	//注意这是成员函数重载了运算符,友元函数重载需要参数
	{
		return buff;
	}
private:
	char* buff;
};

class UStrToWChar		//CStringW ->wchar_t*
{
public:
	UStrToWChar(const CStringW cs)
	{
		int n = cs.GetLength();
		if(n <= 0)
			buff = NULL;
		buff = new wchar_t[n + 1];
		wmemset(buff,0,n + 1);
		for(int i = 0; i < n; i++)
			buff[i] = cs[i];
	}
	~UStrToWChar()
	{
		delete buff;
	}
	const wchar_t* operator &()
	{
		return buff;
	}
private:
	wchar_t* buff;
};

class AStrToAscChar		//CStringA -> char*
{
public:
	AStrToAscChar(const CStringA cs)
	{
		int n = cs.GetLength();
		if(n <= 0)
			buff = NULL;
		buff = new char[n + 1];
		memset(buff,0,n + 1);
		for(int i = 0; i < n; i++)
			buff[i] = cs[i];
	}
	~AStrToAscChar()
	{
		delete buff;
	}
	const char* operator &()
	{
		return buff;
	}
private:
	char* buff;
};

class AscCharToWChar	//char* -> wchar_t*
{
public:
	AscCharToWChar(const char* ac)
	{
		if(ac == NULL || strlen(ac) <= 0)
			buff = NULL;
		int n = MultiByteToWideChar(CP_ACP,0,ac,-1,NULL,0);
		buff = new wchar_t[n];
		wmemset(buff,0,n);
		MultiByteToWideChar(CP_ACP,0,ac,-1,buff,n);
	}
	~AscCharToWChar()
	{
		delete buff;
	}
	const wchar_t* operator &()
	{
		return buff;
	}
private:
	wchar_t* buff;
};

Unicode和多字节之间的转换是通过WIN API完成的。

Unicode和Unicode之间、以及多字节和多字节之间,是通过字符拷贝完成的。

使用方法:

CStringW cs = L"中文English";

UStrToAscChar us(cs);

printf_s("%s \n",&us);  //在控制台输出字符串
关于如何在MFC程序中打开控制台:

class Console
{
public:
	Console()
	{
		AllocConsole();
		freopen_s(&fp,"conout$","w",stdout);	//打开控制台

		setlocale(LC_ALL,"chs");
	}
private:
	FILE *fp;
};

只要添加一个变量即可:Console sole;

你可能感兴趣的:(char,wchar_t,mfc,WTL,CStringA,CStringW,字符串转换,CString)