数据转换

/************************************************************************
函数名:  ByteCStringToDec
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-2-18
作  用:  将十六进制字符串(高位字节在后面:如1F 55, 即0x551F)转换为十进制
形参数:  CString str
返回值:  int
修改记录:                                             
************************************************************************/
int ByteCStringToDec(CString str)
{
	char ch[2];
	int num = 0, n = 0, i, sum = 0;
	str.Replace(" ", "");

	for(i = str.GetLength() - 2; i >= 0; i = i - 4)
	{
		n = 0;
		for(int j = 0; j < 2; j++)
		{
			strcpy(ch, (LPCTSTR)str.Mid(i++, 1));
			if(ch[0] >= '0' && ch[0] <= '9')
			{	
				num = ch[0] - 48;
			}
			else if(ch[0] >= 'A' && ch[0] <= 'F')
			{
				num = ch[0] - 65 + 10;
			}
			else if(ch[0] >= 'a' && ch[0] <= 'f')
			{
				num = ch[0] - 97 + 10;
			}
			n = n * 16 + num;
		}
		sum = sum * 256 + n;
	}

	return sum;
}
/************************************************************************
函数名:  StrToDec
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-2-18
作  用:  将十六进制字符串(如0x551F)或十进制字符串转换为十进制
形参数:  CString strInt
返回值:  int
修改记录:                                             
************************************************************************/
int StrToDec(CString strInt)
{
	int n, i, base;
	n = 0;
	
	strInt.TrimLeft();
	strInt.TrimRight();

	//Hex
	if(strInt.GetLength() > 2 && strInt.GetAt(0) == '0' && (strInt.GetAt(1) == 'x' || strInt.GetAt(1) == 'X'))
	{
		strInt = strInt.Mid(2, strInt.GetLength() - 2);
		base = 16;
	}
	else//Decimal
	{
		base = 10;
	}

	for(i = 0; i < strInt.GetLength(); i++)
	{
		n = n * base + CharToInt(strInt.GetAt(i));
	}

	return n;
}
/************************************************************************
函数名:  DecToMemoryHexCString
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-1
作  用:  将十进制转换为十六进制字符串(如1F 55,即0x551F)
形参数:  
返回值:  
修改记录:                                             
************************************************************************/
CString DecToMemoryHexCString(int n)
{
	int i;
	CString strHex;
	strHex.Format(_T("%0.8X"), n);
	strHex = ReverseByte(strHex);
	
	for(i = 2; i < strHex.GetLength(); i = i + 3)
	{
		strHex.Insert(i, " ");
	}

	if(strHex.Right(5) == "00 00")
	{
		strHex.Delete(5, 6);
	}

	return strHex;
}

/************************************************************************
函数名:  DecToByteStr
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-7
作  用:  将十进制转换为十六进制字符串(如1F,即0x1F)
形参数:  
返回值:  
修改记录:                                             
************************************************************************/
CString DecToByteStr(int n)
{
	CString strHex;
	strHex.Format(_T("%0.2X"), n);
	
	return strHex;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值