mfc学习

mfc

1.让str支持任意编码的中文字符,您可以使用urlencode函数对其进行编码。这样,无论str中的字符是什么编码,它们都将被正确地传递给URL。下面是如何在MFC中实现urlencode的示例:

		CString strl = NULL;
		GetDlgItemText(IDC_EDIT1, str);
CString UrlEncode(const CString& str)
{
    CStringA utf8Str;
    WideCharToMultiByte(CP_UTF8, 0, str.GetString(), -1, utf8Str.GetBuffer(str.GetLength() * 4), str.GetLength() * 4, NULL, NULL);
    utf8Str.ReleaseBuffer();

    CString encodedStr;
    for (int i = 0; i < utf8Str.GetLength(); ++i)
    {
        char ch = utf8Str[i];
        if (isalnum((unsigned char)ch) || ch == '-' || ch == '_' || ch == '.' || ch == '~')
        {
            encodedStr += ch;
        }
        else
        {
            encodedStr.AppendFormat(_T("%%%02X"), (unsigned char)ch);
        }
    }
    return encodedStr;
}
使用方法
CString encodedStr = UrlEncode(str);
pHttpFile = (CHttpFile*)sess.OpenURL(_T("https://sadasahz.top/asad.php?key=daf&shuo=") + encodedStr);

2将中文字符转换为HTML实体

CString ConvertToHtmlEntities(const CString& input)//将中文字符转换为HTML实体
{
	CString output;
	for (int i = 0; i < input.GetLength(); ++i)
	{
		wchar_t ch = input[i];
		if (ch > 127) // Only convert non-ASCII characters
		{
			CString entity;
			entity.Format(L"&#x%X;", static_cast<unsigned int>(ch));
			output += entity;
		}
		else
		{
			output += ch;
		}
	}
	return output;
}
使用方法
CString htmlEntitiesContent = ConvertToHtmlEntities(content);

		std::string command = "mail.exe -u \"" + CStringToStdString(recipient) + "\" -i \"" + sender + "\" -p \"" + password + "\" -s \"Automail\" -b \"" + CStringToStdString(htmlEntitiesContent) + "\"";
	

3将编码转化为Unicode编码发送到Edit contorl控件 // Convert the content to Unicode

			int unicodeLength = MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, NULL, 0);
			wchar_t* unicodeContent = new wchar_t[unicodeLength];
			MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, unicodeContent, unicodeLength);

			strl = unicodeContent;
			delete[] unicodeContent;
		使用方法:
			int unicodeLength = MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, NULL, 0);
			wchar_t* unicodeContent = new wchar_t[unicodeLength];
			MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, unicodeContent, unicodeLength);

			strl = unicodeContent;
			delete[] unicodeContent;
		//MessageBox(strl);
		SetDlgItemText(IDC_EDIT2, strl);

4将Cstring转为操作cmd可以执行的类型

std::string CStringToStdString(const CString& cstr)
{
	CT2CA pszConvertedAnsiString(cstr);
    std::string strStd(pszConvertedAnsiString);
    return strStd;
}
使用		std::string command = "mail.exe -u \"" + CStringToStdString(recipient) + "\" -i \"" + sender + "\" -p \"" + password + "\" -s \"Automail\" -b \"" + CStringToStdString(htmlEntitiesContent) + "\"";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值