

#include "..\comp\TransCod\TrsCdDig.h"

CTransCodeDigivote::CTransCodeDigivote()
    : CTransCode()
{

}


CTransCodeDigivote::~CTransCodeDigivote()
{
}

CTransCode* CTransCodeDigivote::Instance(void)
{
	if(m_pchInst == NULL)
		m_pchInst = new CTransCodeDigivote();
	return m_pchInst;
}

char* CTransCodeDigivote::Decoding(const char* pzParam)
{
    char* pzValue = NULL;
	if( strlen(pzParam) <= (MAX_LENGHT_CHAR * 2))
	{
		unsigned int uiCpt = 0;
		unsigned int uiWrCpt = 0;

		memset(m_szBuffer, 0, sizeof(m_szBuffer));
		while (uiCpt < strlen(pzParam))
		{
			if(pzParam[uiCpt] == '9')
			{
				m_szBuffer[uiWrCpt++] = ' ';
				uiCpt += 2;
			}
			else if(pzParam[uiCpt] == '0')
			{/* character 0 to 9*/
				uiCpt++;
				m_szBuffer[uiWrCpt++] = pzParam[uiCpt++];
			}
			else /* character A to Z */
			{/*Offset=55 becoce A = 0x41*/
				m_szBuffer[uiWrCpt] = 55 + (pzParam[uiCpt++] - 0x30) *10;
				m_szBuffer[uiWrCpt++] += (pzParam[uiCpt++] - 0x30);
			}
		}
		pzValue = m_szBuffer;
	}

    return pzValue;
}


char* CTransCodeDigivote::Encoding(const char* pzParam)
{
    char* pzValue = NULL;

	if( strlen(pzParam) <= MAX_LENGHT_CHAR)
	{
		unsigned int uiCpt = 0;
		unsigned int uiWrCpt = 0;

		/* Clear buffer */
		memset(m_szBuffer, 0, sizeof(m_szBuffer));

		while (uiCpt < strlen(pzParam))
		{
	      /* Encoding one character to two character,
		 0 at 9 => 01 at 09
		 A at Z => 10 at 35
	      */
			if(pzParam[uiCpt] == ' ')
			{
				m_szBuffer[uiWrCpt++] = '9';
				m_szBuffer[uiWrCpt++] = '9';
				uiCpt++;
			}
			else if(pzParam[uiCpt] >= '0' && pzParam[uiCpt] <= '9')
			{/* character 0 to 9*/
				m_szBuffer[uiWrCpt++] = '0';
				m_szBuffer[uiWrCpt++] = pzParam[uiCpt++];
			}
			else /* character A to Z */
			{/*Offset=0x31 becoce A = 0x41*/
			    char szTp[3];
			    sprintf(szTp, "%d", (int)(pzParam[uiCpt++]-55));
			    szTp[2] = 0;
			    strcat(m_szBuffer, szTp);
				uiWrCpt += 2;
			}
		}
		pzValue = m_szBuffer;
	}

    return pzValue;
}

