TLV编解码【编程】

TLV编解码【编程】_第1张图片

感觉做的方法不好,仅用于备份,仅供参考!

不多说了,直接上代码!


#include 
using namespace std;
#define M 10
int dataLength = 0;

struct NODE{
	char strTag[100];	
	char strLen[100];
	char strVale[100];
	bool isNoLen;
	int value;
}nodeData[M];

int GetNumLength(char *ppstr)
{
	char*pstr = strdup(ppstr);
	if(pstr==NULL)
		return 0;
	int num1 = 0;
	int num2 = 0;
	char *pSaveStr = strtok(pstr,",");
	while(pSaveStr!=NULL)
	{		
		sscanf(pSaveStr,"%x",&num2);
		num1 *= (16*16);	
		num1 += num2;		
		pSaveStr = strtok(NULL,",");
	}
	delete pstr;
	return num1;
}
bool GetMsg(char *pstr)
{
	while(pstr!=NULL)
	{	
		char *pSavest1 = pstr+30;
		if(*pSavest1 == '\0')
			return false;
		strncpy(nodeData[dataLength].strTag, pstr, 19);
		strncpy(nodeData[dataLength].strLen, pstr+20, 9);
		int nunLength = GetNumLength(nodeData[dataLength].strLen);
		if(nunLength!=0)
			nodeData[dataLength].isNoLen = false;
		else 
			nodeData[dataLength].isNoLen = true;
		strncpy(nodeData[dataLength].strVale, pstr+30, nunLength*5);
		nodeData[dataLength].value = GetNumLength(nodeData[dataLength].strVale);
		pstr=pSavest1+nunLength*5;	
		dataLength++;
		if(*pstr=='\0')
			return true;	
	}
	return true;
}
void PaiXu()
{
	for (int i=0; i>strInput;
	if(!GetMsg(strInput))
	{
		cout<<"false"<





你可能感兴趣的:(笔试)