⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 GmsModemDll Gms ModemDll 关于GPRS MODEM的AT命令集合
💻 CPP
字号:
/*
* Copyright (c) 2003-2004,Shenzhen TaiJi SoftWare
* All rights reserved.
* 
* Name of the File:			common.cpp 
* symbol of the File:		
* Summary:					string transform
* Current Version:			1.0
* Author:					DengYangjun
* Created Date:				17:07 01-02-2004
* Finished Date:			??
*
* Substitutional Version:	
* Original Author:
* Finished Date:
*/

#include "stdafx.h"
#include "common.h"
#include <winsock2.h>
#include <cmath>
//using namespace std;
/*****************************************************************************
* Class      : Global Function
* Function   : Int2String
* Description: Integer to String
* Parameters : 
* Return     : string that is conversioned
******************************************************************************/
CString Int2String(unsigned int n)
{
	CString sResult;
	sResult.Format("%d", n);

	return sResult;
}
/*****************************************************************************
* Class      : Global Function
* Function   : to_MultiByte
* Description: Unicode To MultiByte
* Parameters : char*	strSM --the Buffer address
*              int	nLength -- data length
* Return     : MultiByte String
******************************************************************************/
CString to_MultiByte(char* strSM, int nLength){
	UINT nLen = 0;
	PBYTE lpszM;
	PBYTE lpszW = new BYTE[nLength];
	memcpy(lpszW, strSM, nLength);

	for(int i = 0; i < nLength/2; i++)
		*((unsigned short*)lpszW + i) = ntohs(*((unsigned short*)lpszW + i));

	nLen = WideCharToMultiByte(936, WC_COMPOSITECHECK,
			(const unsigned short*)lpszW, nLength/2, NULL, 0, NULL, NULL);

	lpszM = new BYTE[nLen+1];

	nLen = WideCharToMultiByte(936, WC_COMPOSITECHECK,
			(const unsigned short*)lpszW, nLength/2, (char*)lpszM, nLen, NULL, NULL);

	lpszM[nLen] = 0;
	CString csSM((LPCTSTR)lpszM, nLen);
	delete lpszM;
	delete lpszW;
	return csSM;
}
/*****************************************************************************
* Class      : Global Function
* Function   : to_UCS2
* Description: MultiByte to UCS2
* Parameters : char*	strSM -- -- the address of MultiByte buffer
*              int	nLength -- data length
* Return     : 
******************************************************************************/
CString to_UCS2(char* strSM, int nLength){
		CString csSM((LPCTSTR)strSM, nLength);
		
		PBYTE	lpszW = NULL;
		UINT	nLen = 0;

		nLen = MultiByteToWideChar(936, MB_PRECOMPOSED,
			(LPCTSTR)csSM, csSM.GetLength(), NULL, 0);

		lpszW = new BYTE[nLen * 2];

		nLen = MultiByteToWideChar(936, MB_PRECOMPOSED, 
				(LPCTSTR)csSM, csSM.GetLength(), (LPWSTR)lpszW, nLen);

		for(UINT i = 0; i < nLen; i ++)
			*((unsigned short*)lpszW + i) = htons(*((unsigned short*)lpszW + i));

		CString csRet((LPCTSTR)lpszW, nLen * 2);
		delete lpszW;
		return csRet;
}

/*****************************************************************************
* Class      : Global Function
* Function   : to_BCD
* Description: 
* Parameters : char* strNormal--要转换的多字节缓冲
*              int	nLength -- 多字节缓冲的长度
* Return     : 转换后的BCD字串
******************************************************************************/
CString to_BCD(char *strNormal, int nLength)
{
	CString sNormal;

	sNormal.Format("%s",strNormal);
	//ASSERT(nLength==sNormal.GetLength());
	nLength=sNormal.GetLength();
	
	if(0!=nLength%2)  //如果字符个数是奇数,在末尾加'F';
	{
		sNormal+='F';
	}
	nLength=sNormal.GetLength();

	CString sResult(sNormal);
	for(int i=0; i<nLength; i++)
	{
		sResult.SetAt(i,'#');
	}
	for( i=0; i<nLength/2; i++)
	{
		sResult.SetAt(2*i, sNormal.GetAt(2*i+1));
	
		sResult.SetAt(2*i+1,sNormal.GetAt(2*i));
	}
	return sResult;
}

/*****************************************************************************
* Class      : Global Function
* Function   : from_BCD
* Description: 把BCD码转换成普通的字符串
* Parameters : char* strBCD--要转换BCD码
*              int	nLength -- 多字节缓冲的长度
* Return     : 转换后的普通字串
******************************************************************************/
CString from_BCD(char *strBCD, int nLength)
{
	CString sResult;
	
	sResult.Format("%s",strBCD);

	for(int i=0; i<nLength; i++)
	{
		sResult.SetAt(i,'#');
	}

	//sResult.Format("%s",strBCD);

	CString sBCD;
	sBCD.Format("%s",strBCD);

	for( i=0; i<nLength/2; i++)
	{
		sResult.SetAt(2*i, sBCD.GetAt(2*i+1));
	
		sResult.SetAt(2*i+1,sBCD.GetAt(2*i));
		
	}
	
	ASSERT(0==nLength%2);
	
	

	return sResult;
}

/*****************************************************************************
* Class      : Global Function
* Function   : from_BCD
* Description: 把整型的数据转化为16进制的CString,数据大小限制在0-255以内
* Parameters : unsigned int n--要转换的整数
* Return     : 转换后的普通字串
******************************************************************************/
CString Int2HexString(BYTE n)
{
	CString sResult;
	if(n<16)
	{
		sResult.Format("0%0x",n);
	}
	else
	{
		sResult.Format("%0x",n);
	}
	return sResult;
}

CString &Exchange(CString &sOrg)
{
	ASSERT(2==sOrg.GetLength());
	CString sTemp;

	char d1=sOrg.GetAt(0);
	char d2=sOrg.GetAt(1);
	sTemp+=d2;
	sTemp+=d1;
	
	sOrg=sTemp;

	//why this statement is eroror??
	//	sOrg.Format("%s%s",sTemp.GetAt(1),sTemp.GetAt(1));

	//sOrg.Format("%s%s",sOrg.GetAt(1),sOrg.GetAt(0));
	return sOrg;
}


/*****************************************************************************
* Class      : Global Function
* Function   : from_BCD
* Description: 它把16进制ACS2字符转化为INT
* Parameters : const char cOrg--要转换的字符串
* Return     : 转换后的整数值
******************************************************************************/
int ctoi(const char cOrg)
{
	int bResult=0;

	//is Digit
	if(0!=isdigit(cOrg))
	{
		bResult=cOrg-48;
		return bResult;
	}
	
	switch(cOrg)
	{
	case 'a':
	case 'A':
		{
			bResult=10;
			break;
		}
	case 'b':
	case 'B':
		{
			bResult=11;
			break;
		}
	case 'c':
	case 'C':
		{
			bResult=12;
			break;
		}
	case 'd':
	case 'D':
		{
			bResult=13;
			break;
		}
	case 'e':
	case 'E':
		{
			bResult=14;
			break;
		}
	case 'f':
	case 'F':
		{
			bResult=15;
			break;
		}
	
	default:
		{
			ASSERT(FALSE);
			break;
		}

	}
	return bResult;
}

/*****************************************************************************
* Class      : Global Function
* Function   : from_BCD
* Description: 它把2个字节长度16进制ACS2字符串转化为INT
* Parameters : const char cOrg--要转换的字符串
* Return     : 转换后的整数值
******************************************************************************/
int ahtoi(const char *pOrg)
{
	int bResult=0;
	CString sOrg(pOrg);

	ASSERT(2==sOrg.GetLength());
	
	//先高位
	bResult=ctoi(sOrg.GetAt(0));
	
	//后低位
	int dd=ctoi(sOrg.GetAt(1));
	bResult=bResult*16+dd;

	return bResult;
}


/*****************************************************************************
* Class      : Global Function
* Function   : From7Bit
* Description: 将7位的PDU编码转换成正常的ACS2字符
* Parameters : s7BitData:7位的PDU编码的字符串
				nCount: acs2字符的个数
* Return     : 正常显示的ACS2字符串,解析失败返回空字符串
******************************************************************************/
CString From7Bit(CString s7BitData, UINT nCount)
{
	CString result;
	//1.验证有效性
	UINT length=s7BitData.GetLength();
	if(length*7 < nCount*8 )
	{
		return result; //不合法,返回空字符串
	}	
	
	//2.把16进制的字符串转化成二进制的字符串
	s7BitData=Hex2Binary(s7BitData);

	CString singleByte;	//8位的2进制数
	CString spareLetter; //每行多余的字符
	int countLine=0;
	
	int total=s7BitData.GetLength();

	int letterValue;

	char resolvedLetter;  //已经解析到的字符
	for(;;)
	{	
		singleByte=s7BitData.Mid(countLine*8,8); //

		total-=8; //总的字符数量-8

		CString newSingleByte=singleByte.Right(8-(countLine+1)%7);

		newSingleByte+=spareLetter; //加上一行剩余的字符

		/*
		if((countLine+1)%8==0)
		{
			newSingleByte=newSingleByte.Right(7);
			spareLetter="1";
		}
		*/
		
		if(newSingleByte.GetLength()==14)
		{
			letterValue=0;

			for(int i=7; i<14; i++)
			{
				letterValue+=ctoi(newSingleByte.GetAt(i))*(int)pow(2,14-i)/2;
			}

			resolvedLetter=letterValue;
			result+=resolvedLetter; //增加结果中去
		}

		letterValue=0;

		for(int i=0; i<7; i++)
		{
			letterValue+=ctoi(newSingleByte.GetAt(i))*(int)pow(2,7-i)/2;
		}

		resolvedLetter=letterValue;
		result+=resolvedLetter; //增加结果中去

		
		//得到每行剩余的字符,下一次循环使用
		spareLetter=singleByte.Left((countLine+1)%7);
		
		if(total<=8)
		{
			CString newSingleByte;
			newSingleByte=s7BitData.Right(total-nCount%8);
			newSingleByte+=spareLetter;

			
			if(newSingleByte.GetLength()<7)
			{
				return result;
			}

			letterValue=0;

			for(int i=0; i<7; i++)
			{
				letterValue+=ctoi(newSingleByte.GetAt(i))*(int)pow(2,7-i)/2;
			}

			resolvedLetter=letterValue;
			result+=resolvedLetter; //增加结果中去

			break; //完成了解析,退出循环
		}
		
		//解析下一行
		countLine++;
		
				
	}
	return result;

}

CString Hex2Binary(CString HexString)
{
	CString result; //结果字符串

	char cTemp;
	int iHexValue;
	CString singleLetter;//一个字符的结果
	for(int i=0; i<HexString.GetLength(); i++)
	{
		singleLetter="";
		cTemp=HexString.GetAt(i);
		iHexValue=ctoi(cTemp);
		
		//得到最高位
		if(iHexValue>=8)
		{
			singleLetter="1";
			iHexValue-=8;
		}
		else
		{
			singleLetter="0";
		}

		if(iHexValue>=4)
		{
			singleLetter+="1";
			iHexValue-=4;
		}
		else
		{
			singleLetter+="0";
		}

		if(iHexValue>=2)
		{
			singleLetter+="1";
			iHexValue-=2;
		}
		else
		{
			singleLetter+="0";
		}

		if(iHexValue>=1)
		{
			singleLetter+="1";
			
		}
		else
		{
			singleLetter+="0";
		}
		
		result+=singleLetter;

	}//for for

	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -