Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Apr 24, 2001

CString - IPAddress(DWORD) Conversion for CIPAddressCtrl


The following method returns a (DWORD)IPAddress from a (CString)IPAddress. It is quite useful when using the CIPAddressControl.
 
DWORD CMyClass::GetDWORDIPAddress(CString strIPAddress)
{
	strIPAddress.MakeReverse();// Start from behind

	char DOT = '.';
	DWORD dwReturnValue = 0;

	double dPower = 0.0;

	int length = strIPAddress.GetLength();

	DWORD dwByteTemp = 0;
	int nWhichByte = 0;
	int i = 0;
	for(i; i<length ; i++ ){
		volatile int nTemp = 0;
		char aChar = strIPAddress.GetAt(i);

		if(aChar != DOT){
			int nChar = 0;
			switch(aChar){
				case '1': nChar = 1; break;
				case '2': nChar = 2; break;
				case '3': nChar = 3; break;
				case '4': nChar = 4; break;
				case '5': nChar = 5; break;
				case '6': nChar = 6; break;
				case '7': nChar = 7; break;
				case '8': nChar = 8; break;
				case '9': nChar = 9; break;
				case '0': nChar = 0; break;
				default: break;
			}
			nTemp = nChar * (int)pow(10.0 ,dPower);
			dwByteTemp += nTemp;
			dPower++;

			if(i == length-1/*Last Byte*/){
				dwByteTemp <<= (nWhichByte * 8);// 
8 Bits = Byte Length
				dwReturnValue = dwReturnValue | dwByteTemp;
				dPower = 0;
				dwByteTemp = 0;
				nWhichByte++;
			}
		} else {
			dwByteTemp <<= (nWhichByte * 8);// 8 Bits = 
Byte Length
			dwReturnValue = dwReturnValue | dwByteTemp;
			dPower = 0;
			dwByteTemp = 0;
			nWhichByte++;
		}
	}
	strIPAddress.MakeReverse();//Undo
	return dwReturnValue;
}
Roland Fru
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date