首页
社区
课程
招聘
[原创]发些个人库之 字符串分割,组合,编码转换,bin2str,str2bin
发表于: 2013-10-7 12:26 10245

[原创]发些个人库之 字符串分割,组合,编码转换,bin2str,str2bin

2013-10-7 12:26
10245
#pragma once

#ifndef _AFX
#ifndef __ATLSTR_H__
#include <atlstr.h>
#endif
#endif

/*
	CString		strRet;
	LPBYTE		hex[10];

	XLIB::CXString s;
	XLIB::CXStringA sA;
	XLIB::CXStringW sW;

	strRet = _T("AA");

	sA = "af af af 11 11 12";

	sA = " a b ";

	XLIB::CXStringA pList[4];
	sA.Split( _T("\0x09"),pList,4 );
	sW =sA;



	sW.ToHex( hex,10,1 );
	sW.FromHex(hex,10,L"A");
	
	sA= "";
	sA = _T("");
	sA = L"";
	sW= "";
	sW = _T("");
	sW = L"";

	s= "nnn";
	s = _T("");
	s = L"";

	s = strRet;
	sW = strRet;
	sA = strRet;

	sA += _T("AA");

	strRet = s ;
	strRet = sA;
	strRet = sW;

	sW = sA.ToUnicode(CP_UTF8);
	*/


namespace XLIB
{


template<class _BaseString>
class TXStringBase:public _BaseString
{
public:

	typedef TXStringBase<_BaseString> _Myt;

	TXStringBase(){};

	template<class _IT0>	
		TXStringBase(_IT0 it0):_BaseString(it0){}
	template<class _IT0,class _IT1>
		TXStringBase(_IT0 it0,_IT1 it1):_BaseString(it1){}

	template<class _It>
		_Myt& operator=(_It _Ptr){ return (_Myt&)_BaseString::operator=(_Ptr);};
	
	_Myt& operator=(const TXStringBase<CStringA> &_Ptr){ return (_Myt&)_BaseString::operator=(_Ptr);};
	_Myt& operator=(const TXStringBase<CStringW> &_Ptr){ return (_Myt&)_BaseString::operator=(_Ptr);};

	template<class _It>
		operator	_It(){return _BaseString::operator _It();};

	TXStringBase<CStringA>	ToAnsi( UINT tocodepage = CP_ACP,UINT fromcodepage=CP_ACP );
	TXStringBase<CStringW>	ToUnicode( UINT fromcodepage=CP_ACP );

	long	ToLong();

	int		ToHex( LPVOID lpBuffer,int len,int lpSeparate = 0);//返回转换后的长度lpSeparate为间隔字符的长度
	int		FromHex( LPCVOID lpBuffer,int len,TXStringBase<_BaseString> lpSeparate = L"" );

	int		Split(_BaseString separate,TXStringBase<_BaseString>* pStrlist = 0,int listlen = 0);//返回需要多少个list

};

template<>
inline long TXStringBase<CStringA>::ToLong()
{
	return atol(GetBuffer(0));
}

template<>
inline long TXStringBase<CStringW>::ToLong()
{
	return _wtol(GetBuffer(0));
}

template<class _BaseString>
int	TXStringBase<_BaseString>::Split(_BaseString separate,TXStringBase<_BaseString>* pStrlist,int listlen )//返回转换后的长度
{
	int		posBegin,posEnd;
	int		Count = 0;

	posEnd = -1;
	do 
	{
		posBegin = posEnd;

		posEnd = this->Find( separate,-1==posBegin?0:posBegin+1);
		if( -1 == posEnd )posEnd=this->GetLength();

		
		if( posBegin )posBegin++;

		if( pStrlist && listlen > Count)
		{
			pStrlist[Count] = this->Mid( posBegin,posEnd-posBegin);

		}

		Count++;
	} while (posEnd!=this->GetLength());
	
	return Count;
}

template<class _BaseString>
int	TXStringBase<_BaseString>::ToHex( LPVOID lpBuffer,int len,int lpSeparate )//返回转换后的长度
{
	char	ch;
	LPBYTE	lpOut;
	int		i;
	BYTE	value;

	lpOut = (LPBYTE)lpBuffer;

	for( i = 0;i<len;i++)
	{
		if( GetLength() < 2*i+i*lpSeparate )break;

		ch = (char)GetAt(2*i+i*lpSeparate);
		if( 0 == ch )break;

		if( ch >= '0' && ch <='9' )
		{
			value = ch-'0';
			lpOut[i] = value<<4;
		}else if( (ch >= 'a' && ch <='f') || (ch >= 'A' && ch <='F') )
		{
			value = ch |( 'a'-'A');
			value -= 'a';
			value += 0x0a;
			lpOut[i] = value<<4;
		}else lpOut[i] = 0;

		if( GetLength() < 2*i+i*lpSeparate+1 )break;
		ch = (char)GetAt(2*i+i*lpSeparate+1);
		if( 0 == ch )break;

		if( ch >= '0' && ch <='9' )
		{
			value = ch-'0';
			lpOut[i] |= value&0xF;
		}else if( (ch >= 'a' && ch <='f') || (ch >= 'A' && ch <='F') )
		{
			value = ch |( 'a'-'A');
			value -= 'a';
			value += 0x0a;
			lpOut[i] |= value&0xF;
		}
	}

	return i;
}

template<class _BaseString>
int	TXStringBase<_BaseString>::FromHex( LPCVOID lpBuffer,int len,TXStringBase<_BaseString> lpSeparate )//返回转换后的长度
{
	WCHAR	hex[]=L"0123456789abcdefg";
	BYTE	hi;
	TXStringBase<_BaseString>	temp;
	LPBYTE	lpinput;
	int		i;

	this->Empty();

	lpinput = (LPBYTE)lpBuffer;
	
	for( i = 0;i<len;i++)
	{
		hi = lpinput[i]>>4;

		temp = hex[hi];
		this->Append( temp);

		hi = lpinput[i]&0xF;
		temp = hex[hi];
		this->Append( temp);

		if( i <len - 1 )
		this->Append(lpSeparate);
	}

	return GetLength();
}
template<>
inline TXStringBase<CStringA>	TXStringBase<CStringA>::ToAnsi( UINT tocodepage,UINT fromcodepage )
{
	TXStringBase<CStringW>	strW;

	strW = ToUnicode(fromcodepage);

	return strW.ToAnsi(tocodepage);
}

template<>
inline TXStringBase<CStringA>	TXStringBase<CStringW>::ToAnsi( UINT tocodepage,UINT fromcodepage )
{
	TXStringBase<CStringA>	strRetA;
	int						nDestLength;
	char*					pszDest;

	nDestLength = ::WideCharToMultiByte(tocodepage, 0, GetBuffer(0), GetLength(), NULL, 0, NULL, NULL);
	if( nDestLength < 1 )return strRetA;

	pszDest = strRetA.GetBuffer(nDestLength);
	nDestLength = ::WideCharToMultiByte(tocodepage, 0, GetBuffer(0), GetLength(), pszDest, nDestLength, NULL, NULL);

	strRetA.ReleaseBufferSetLength(nDestLength);

	return strRetA;
}

template<>
inline TXStringBase<CStringW>	TXStringBase<CStringA>::ToUnicode( UINT fromcodepage )
{
	TXStringBase<CStringW>	strRetW;
	int						nDestLength;
	WCHAR*					pszDest;

	nDestLength = ::MultiByteToWideChar(fromcodepage, 0, GetBuffer(0), GetLength(), NULL, 0);
	if( nDestLength < 1 )return strRetW;

	pszDest = strRetW.GetBuffer(nDestLength);
	nDestLength = ::MultiByteToWideChar(fromcodepage, 0, GetBuffer(0), GetLength(), pszDest, nDestLength);

	strRetW.ReleaseBufferSetLength(nDestLength);

	return strRetW;	
}

template<>
inline TXStringBase<CStringW>	TXStringBase<CStringW>::ToUnicode( UINT fromcodepage )
{
	return *this;
}

typedef TXStringBase<CStringW> CXStringW;
typedef TXStringBase<CStringA> CXStringA;
typedef TXStringBase<CString> CXString;


template<class _BaseString>
class TXStringListBase
{
public:
	TXStringListBase()
	{
		m_pList = 0;
		m_ListCount = 0;
	}
	TXStringListBase(const TXStringListBase& list)
	{
		Create(list);
	}
	~TXStringListBase()
	{
		Close();
	}

	BOOL Create(int count)
	{	
		Close();
		m_ListCount=count;
		if(m_ListCount)m_pList = new _BaseString[m_ListCount];
		return TRUE;
	}
	BOOL Create(const TXStringListBase& list){Dump(list);return TRUE;}

	TXStringListBase&  Dump(const TXStringListBase& list)
	{
		Close();
		m_ListCount=list.GetCount();
		m_pList = new _BaseString[m_ListCount];
		for( int i = 0;i<m_ListCount;i++)m_pList[i] = list.m_pList[i];
		return *this;
	}

	VOID Close()
	{
		if(m_pList)delete[] m_pList;
		m_pList = 0;
		m_ListCount = 0;
	}

	TXStringListBase& operator = (const TXStringListBase& list)
	{
		return Dump(list);
	}

	_BaseString& operator [](int index)
		{ return At(index);}

	_BaseString& At(int index)
	{
		if( index >=0 && index < m_ListCount )
		{

			return m_pList[index];
		}

		m_Default.Empty();

		return m_Default;
	}

	int GetCount(){return m_ListCount;}

	_BaseString Combine( _BaseString separeate )//组合
	{
		_BaseString strRet;
		int i;
		for( i = 0;i<m_ListCount-1;i++)
		{
			strRet += m_pList[i];
			strRet += separeate;
		}
		strRet += m_pList[i];
		return strRet;
	}


	int Split(_BaseString source,_BaseString separeate)
	{
		int nCount = source.Split(separeate);
		if( 0 == nCount )return 0;

		Create(nCount);

		nCount = source.Split(separeate,m_pList,m_ListCount);

		return nCount;
	}
private:
	_BaseString		m_Default;
	_BaseString*	m_pList;
	int				m_ListCount;
};
typedef TXStringListBase<CXStringW> CXStringListW;
typedef TXStringListBase<CXStringA> CXStringListA;
typedef TXStringListBase<CXString> CXStringList;


}


[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 5
支持
分享
最新回复 (5)
雪    币: 80
活跃值: (109)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
2
一直不明白template<>是啥。唉,学识少真不好
2013-10-7 13:42
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
3
特例化,
比如
template<_TYPE>
bool fun(_type a )
{
return true;
}

//特例化int,也就是说对int进行特殊处理
template<>
bool fun(int a)
{

return false;
}
2013-10-7 13:43
0
雪    币: 541
活跃值: (654)
能力值: ( LV12,RANK:250 )
在线值:
发帖
回帖
粉丝
4
不错收藏先
2013-10-7 14:19
0
雪    币: 80
活跃值: (109)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
调用的时候和平时一样?
2013-10-7 16:32
0
雪    币: 239
活跃值: (190)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
6
yes..................
2013-10-7 17:08
0
游客
登录 | 注册 方可回帖
返回
//