c++ 格式转换

bool CFormatConvertDlg::StringConversion(TCHAR* strSource, TCHAR* strDest, int nType)
{
	// TODO: 在此处添加实现代码.
	int nIndex = 0;
	if (nType == 0)//0表示转换成大写
	{
		while (strSource[nIndex] != '\0')
		{
			int nRet = 0;
#ifdef _UNICODE
			nRet = iswlower(strSource[nIndex]);
#else
			nRet = islower(strSource[nIndex]);
#endif // _UNICODE
			if (0 != nRet)
			{
#ifdef _UNICODE
				strDest[nIndex] = towupper(strSource[nIndex]);
#else
				strDest[nIndex] = toupper(strSource[nIndex]);

#endif // _UNICODE

			}
			else
			{
				strDest[nIndex] = strSource[nIndex];
			}
			nIndex++;
		}
		strDest[nIndex] = '\0';
		return true;
	}
	else if (nType == 1) //转换成大写
	{
		while (strSource[nIndex] != '\0')
		{
			int nRet = 0;
#ifdef _UNICODE
			nRet = iswupper(strSource[nIndex]);
#else
			nRet = isupper(strSource[nIndex]);
#endif // _UNICODE
			if (0 != nRet)
			{
#ifdef _UNICODE
				strDest[nIndex] = towlower(strSource[nIndex]);
#else
				strDest[nIndex] = tolower(strSource[nIndex]);

#endif // _UNICODE

			}
			else
			{
				strDest[nIndex] = strSource[nIndex];
			}
			nIndex++;
		}
		strDest[nIndex] = '\0';
		return true;
	}

	return false;
}






void CFormatConvertDlg::OnBnClickedBtnConvertstringnum()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (m_nConversionRule1 == -1)
	{
	/*	MessageBox("请选择转换规则");*/
		return;
	}
	if (m_strSource1.IsEmpty())
	{
		/*MessageBox("请输入待转换的字符串");*/
		return;
	}
	m_strDest1.Empty();
	int n = -1;
	long ln = -1;
	CString str;
	switch (m_nConversionRule1)
	{
	case 0:
		m_strDest1.Format("%d", atoi(m_strSource1));
		break;
	case 1:
		m_strDest1.Format("%ld", atol(m_strSource1));
		break;
	case 2:
		m_strDest1.Format("%.2f", atof(m_strSource1));
		break;
	default:
		break;
	}
	UpdateData(false);
}


void CFormatConvertDlg::OnBnClickedBtnUpperlower()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (m_nConversionRule2 == -1)
	{
		/*MessageBox("请选择转换规则");*/
		return;
	}
	if (m_strSource2.IsEmpty())
	{
	/*	MessageBox("请输入待转换的字符串");*/
		return;
	}
	m_strDest2.Empty();
	TCHAR szBuf1[MAX_PATH];
	TCHAR szBuf2[MAX_PATH];

	//strcpy_s(szBuf1, m_strSource2);//将CString转换成TCHAR
	StringConversion(szBuf1, szBuf2, m_nConversionRule2);
	m_strDest2 = szBuf2;
	UpdateData(false);
}


void CFormatConvertDlg::OnBnClickedBtnConvertansiunicode()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (m_nConversionRule3 == -1)
	{
		MessageBox("请选择转换规则");
		return;
	}
	if (m_strSource3.IsEmpty())
	{
		MessageBox("请输入待转换的字符串");
		return;
	}

	wchar_t* pwstr = NULL;
	int WideCharLen = -1;

	char* pbytes = NULL;
	int MultiByteLen = -1;


	//0,1只能存在一种,根据项目的编码设置来选择
	switch (m_nConversionRule3)
	{
	case 0:
		wchar_t szwDest1[128];
		WideCharLen = MultiByteToWideChar(CP_ACP, 0, m_strSource3, -1, NULL, 0);
		pwstr = new wchar_t[WideCharLen];
		wmemset(pwstr, 0, WideCharLen);
		MultiByteToWideChar(CP_ACP, 0, m_strSource3, -1, pwstr, WideCharLen);

		m_strDest3 = pwstr;
		delete[] pwstr;
		pwstr = NULL;

		break;
	case 1:
		MultiByteLen = WideCharToMultiByte(CP_ACP, 0, (LPCWCH)m_strSource3.GetBuffer(0), -1, NULL, 0, NULL, 0);

		pbytes = new char[MultiByteLen];
		memset(pbytes, 0, MultiByteLen);

		WideCharToMultiByte(CP_ACP, 0, (LPCWCH)m_strSource3.GetBuffer(0), -1, pbytes, MultiByteLen, NULL, NULL);

		m_strDest3 = pbytes;
		delete[] pbytes;
		pbytes = NULL;
		break;
	default:
		break;
	}
	UpdateData(FALSE);


}

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/113881849