
#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int ConvertHexChar(char ch);

int String2Hex(char *SendStr, char *SendHex) 
{
	int hexdata,lowhexdata;
	int hexdatalen=0;
	int len=strlen(SendStr);
	for(int i=0;i<len;)
	{
		char lstr,hstr=SendStr[i];
		if((hstr==' ')||(hstr=='\r')||(hstr=='\n'))
		{                       
			i++;
			continue;
		}

		i++;

		if(i!=len)
		{
           lstr=SendStr[i];
		}
		
		if((i==len)||(lstr==' ')||(lstr=='\r')||(lstr=='\n'))
		{
			lstr=hstr;
			hstr='0';
		}

		hexdata=ConvertHexChar(hstr);
		lowhexdata=ConvertHexChar(lstr);
		if((hexdata>=16)||(lowhexdata>=16))
		{
			return 0;
		}
		else 
			hexdata=hexdata*16+lowhexdata;
		i++;
		SendHex[hexdatalen]=(char)hexdata;
		hexdatalen++;
	}
	return hexdatalen;
}


int ConvertHexChar(char ch)  
{
	if((ch>='0')&&(ch<='9'))
		return int(ch-0x30);
	else if((ch>='A')&&(ch<='F'))
		return int(ch-'A'+10);
	else if((ch>='a')&&(ch<='f'))
		return int(ch-'a'+10);
	else 
		return 256;
}
