C++ 编码转换 UTF8 UrlEncode 宽字符 大写

const wchar_t * hexenc[] = {
		   L"%00", L"%01", L"%02", L"%03", L"%04", L"%05", L"%06", L"%07",
		   L"%08", L"%09", L"%0A", L"%0B", L"%0C", L"%0D", L"%0E", L"%0F",
		   L"%10", L"%11", L"%12", L"%13", L"%14", L"%15", L"%16", L"%17",
		   L"%18", L"%19", L"%1A", L"%1B", L"%1C", L"%1D", L"%1E", L"%1F",
		   L"%20", L"%21", L"%22", L"%23", L"%24", L"%25", L"%26", L"%27",
		   L"%28", L"%29", L"%2A", L"%2B", L"%2C", L"%2D", L"%2E", L"%2F",
		   L"%30", L"%31", L"%32", L"%33", L"%34", L"%35", L"%36", L"%37",
		   L"%38", L"%39", L"%3A", L"%3B", L"%3C", L"%3D", L"%3E", L"%3F",
		   L"%40", L"%41", L"%42", L"%43", L"%44", L"%45", L"%46", L"%47",
		   L"%48", L"%49", L"%4A", L"%4B", L"%4C", L"%4D", L"%4E", L"%4F",
		   L"%50", L"%51", L"%52", L"%53", L"%54", L"%55", L"%56", L"%57",
		   L"%58", L"%59", L"%5A", L"%5B", L"%5C", L"%5D", L"%5E", L"%5F",
		   L"%60", L"%61", L"%62", L"%63", L"%64", L"%65", L"%66", L"%67",
		   L"%68", L"%69", L"%6A", L"%6B", L"%6C", L"%6D", L"%6E", L"%6F",
		   L"%70", L"%71", L"%72", L"%73", L"%74", L"%75", L"%76", L"%77",
		   L"%78", L"%79", L"%7A", L"%7B", L"%7C", L"%7D", L"%7E", L"%7F",
		   L"%80", L"%81", L"%82", L"%83", L"%84", L"%85", L"%86", L"%87",
		   L"%88", L"%89", L"%8A", L"%8B", L"%8C", L"%8D", L"%8E", L"%8F",
		   L"%90", L"%91", L"%92", L"%93", L"%94", L"%95", L"%96", L"%97",
		   L"%98", L"%99", L"%9A", L"%9B", L"%9C", L"%9D", L"%9E", L"%9F",
		   L"%A0", L"%A1", L"%A2", L"%A3", L"%A4", L"%A5", L"%A6", L"%A7",
		   L"%A8", L"%A9", L"%AA", L"%AB", L"%AC", L"%AD", L"%AE", L"%AF",
		   L"%B0", L"%B1", L"%B2", L"%B3", L"%B4", L"%B5", L"%B6", L"%B7",
		   L"%B8", L"%B9", L"%BA", L"%BB", L"%BC", L"%BD", L"%BE", L"%BF",
		   L"%C0", L"%C1", L"%C2", L"%C3", L"%C4", L"%C5", L"%C6", L"%C7",
		   L"%C8", L"%C9", L"%CA", L"%CB", L"%CC", L"%CD", L"%CE", L"%CF",
		   L"%D0", L"%D1", L"%D2", L"%D3", L"%D4", L"%D5", L"%D6", L"%D7",
		   L"%D8", L"%D9", L"%DA", L"%DB", L"%DC", L"%DD", L"%DE", L"%DF",
		   L"%E0", L"%E1", L"%E2", L"%E3", L"%E4", L"%E5", L"%E6", L"%E7",
		   L"%E8", L"%E9", L"%EA", L"%EB", L"%EC", L"%ED", L"%EE", L"%EF",
		   L"%F0", L"%F1", L"%F2", L"%F3", L"%F4", L"%F5", L"%F6", L"%F7",
		   L"%F8", L"%F9", L"%FA", L"%FB", L"%FC", L"%FD", L"%FE", L"%FF"
};
CString CWeb::URLEncode(CString url)
{
	std::wstring text = url;

	size_t len = text.length();
	std::wstring encoded = L"";
	for (size_t i = 0; i < len; i++)
	{
		wchar_t wch = text.at(i);
		if ('A' <= wch && wch <= 'Z') {
			encoded += wch;
		}
		else if ('a' <= wch && wch <= 'z') {
			encoded += wch;
		}
		else if ('0' <= wch && wch <= '9') {
			encoded += wch;
		}
		else if (wch == ' ') {
			encoded += hexenc[wch];
		}
		else if (wch == '-' || wch == '_'
			|| wch == '.' || wch == '!'
			|| wch == '~' || wch == '*'
			|| wch == '\'' || wch == '('
			|| wch == ')') {
			encoded += hexenc[wch];
		}
		else if (wch <= 0x007f) {        // other ASCII
			encoded += hexenc[wch];
		}
		else if (wch <= 0x07FF) {        // non-ASCII <= 0x7FF
			encoded += hexenc[0xc0 | (wch >> 6)];
			encoded += hexenc[0x80 | (wch & 0x3F)];
		}
		else {                    // 0x7FF < ch <= 0xFFFF
			encoded += hexenc[0xe0 | (wch >> 12)];
			encoded += hexenc[0x80 | ((wch >> 6) & 0x3F)];
			encoded += hexenc[0x80 | (wch & 0x3F)];
		}
	}
	return encoded.c_str();
}
CString  CWeb::UrlDecode(CString encodeUrl)
{
	std::wstring text = encodeUrl;
	std::wstring decoded = L"";
	wchar_t temp[] = L"0x00";
	size_t len = text.length();
	int sequence = 0;
	wchar_t conwch = 0;
	for (size_t i = 0; i < len; i++)
	{
		wchar_t wch = text.at(i++);
		if ((wch == '%') && (i + 1 < len))
		{
			temp[2] = text.at(i++);
			temp[3] = text.at(i);
			long tconwch = wcstol(temp, NULL, 16);
			if (tconwch <= 0x7F) {
				decoded += tconwch; // normal ascii char
			}
			else if (tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte
				tconwch = tconwch & 0x3F;
				if (sequence-- == 2)
					tconwch = tconwch << 6;
				conwch |= tconwch;
				if (sequence == 0)
					decoded += conwch;
			}
			else if (tconwch >= 0xC0 && tconwch <= 0xDF) {
				conwch = (tconwch & 0x1F) << 6; // make space for partial bytes
				sequence = 1; // 1 more partial bytes follow
			}
			else if (tconwch >= 0xE0 && tconwch <= 0xEF) {
				conwch = (tconwch & 0xF) << 12; // make space for partial bytes
				sequence = 2; // 2 more partial bytes follow
			} // TODO add case fore 3 partial bytes ... very rare
		}
		else {
			decoded += text.at(--i);
		}
	}
	return decoded.c_str();
}

测试通过!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值