获取特定格式的字符串的地址和数据


函数的作用:

这个函数主要的作用是获取特定格式的字符串的地址和数据

比如  CString strValue = "0x1000:0x2540;0x1001:0x1234;";

地址是0x1000和0x1001,分别需要获取0x2540和0x1234


函数的实现:


int GetCurrentProjectOption(WORD* pwOption, int iMaxCount)
{
	int iCount = 0;	
	int iCopFirstAddr = 0x1000;
	CString strValue = "0x1000:0x2540;0x1001:0x1234;";
	CString strItem;
	int iEndIndex = 0;
	memset(pwOption, 0 ,iMaxCount*sizeof(WORD));
	while (strValue.GetLength() > 0)
	{
		iEndIndex = strValue.Find(_T(";"));
		if(iEndIndex < 0) iEndIndex = strValue.GetLength();
		strItem  = strValue.Left(iEndIndex + 1);
		strItem.Remove(_T(';'));

		int   iAddress = 0;
		TCHAR tszData[100] = {0};
		TCHAR tszAddress[100] = {0};
		//格式的转换,通过这个函数截取不同的字符串
		_stscanf(strItem, _T("0x%[^:]:0x%s"), tszAddress, tszData);
		//字符串转为long型
		iAddress = _tcstol(tszAddress, NULL, 16) - iCopFirstAddr;
		if(iAddress >= 0 && iAddress < (int)(iMaxCount * sizeof(WORD)))
		{			
			pwOption[iAddress] = (WORD)_tcstol(tszData, NULL, 16);
		}
		if((iEndIndex + 1) >= strValue.GetLength()) break;
		//通过之前的分号截取后面的字符串
		strValue = strValue.Mid(iEndIndex + 1);
	}
	return iCount;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值