关闭

A very simple MFC class to Encode and Decode an url string

标签: stringmfcurlclassmodulenull
1056人阅读 评论(1) 收藏 举报
分类:
A very simple MFC class to Encode and Decode an url string

 
Tis is my contribution to encode and decode an url string; My objective was simplifying source code, using CString existing functions...
/*****************************************************************************
Module :     UrlString.h
Notices:     Written 2006 by Stephane Erhardt
Description: H URL Encoder/Decoder
*****************************************************************************/
#ifndef __CURLSTRING_H_
#define __CURLSTRING_H_

class CUrlString
{
private:
	CString m_csUnsafe;

public:
	CUrlString();
	virtual ~CUrlString() { };
	CString Encode(CString csDecoded);
	CString Decode(CString csEncoded);
};

#endif //__CURLSTRING_H_

/*****************************************************************************
Module :     UrlString.cpp
Notices:     Written 2006 by Stephane Erhardt
Description: CPP URL Encoder/Decoder
*****************************************************************************/
#include "stdafx.h"
#include "UrlString.h"

/*****************************************************************************/
CUrlString::CUrlString()
{
	m_csUnsafe = _T("%=/"<>//^[]`+$,@:;/!#?&'");
	for(int iChar = 1; iChar < 33; iChar++)
		m_csUnsafe += (char)iChar;
	for(int iChar = 124; iChar < 256; iChar++)
		m_csUnsafe += (char)iChar;
}

/*****************************************************************************/
CString CUrlString::Encode(CString csDecoded)
{
	CString csCharEncoded, csCharDecoded;
	CString csEncoded = csDecoded;

	for(int iPos = 0; iPos < m_csUnsafe.GetLength(); iPos++)
	{
		csCharEncoded.Format(_T("%%%02X"), m_csUnsafe[iPos]);
		csCharDecoded = m_csUnsafe[iPos];
		csEncoded.Replace(csCharDecoded, csCharEncoded);
	}
	return csEncoded;
}

/*****************************************************************************/
CString CUrlString::Decode(CString csEncoded)
{
	CString csUnsafeEncoded = Encode(m_csUnsafe);
	CString csDecoded = csEncoded;
	CString csCharEncoded, csCharDecoded;

	for(int iPos = 0; iPos < csUnsafeEncoded.GetLength(); iPos += 3)
	{
		csCharEncoded = csUnsafeEncoded.Mid(iPos, 3);
		csCharDecoded = (char)strtol(csUnsafeEncoded.Mid(iPos + 1, 2), NULL, 16);
		csDecoded.Replace(csCharEncoded, csCharDecoded);
	}
	return csDecoded;
}
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18722次
    • 积分:273
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:2篇
    • 译文:0篇
    • 评论:4条