Files
2022-10-26 12:25:11 +08:00

56 lines
1.0 KiB
C++

#include "StdAfx.h"
#include ".\changeip.h"
#include <string>
ChangeIP::ChangeIP(void)
{
}
ChangeIP::~ChangeIP(void)
{
}
BOOL ChangeIP::GetIPValue( const TCHAR* pszIP, DWORD& dwKey )
{
if( !pszIP ) return FALSE;
int nCnt = 0;
BYTE byIP[4];
std::string strIP = pszIP;
while( nCnt < 4 )
{
int nIdx = (int)strIP.find( '.' );
if( nIdx == std::string::npos )
{
if( strIP.size() > 0 )
byIP[nCnt] = atoi( strIP.c_str() );
break;
}
std::string strTemp = strIP.substr( 0, nIdx );
byIP[nCnt] = atoi( strTemp.c_str() );
strIP.erase( 0, strTemp.size() + 1 );
nCnt++;
}
dwKey = ( (DWORD)byIP[0] << 24 | (DWORD)byIP[1] << 16 | (DWORD)byIP[2] << 8 | (DWORD)byIP[3] );
return TRUE;
}
BOOL ChangeIP::GetStrIP( DWORD dwIPKey, TCHAR* pszIP, int nBufSize )
{
if( !pszIP ) return FALSE;
BYTE byIP[4];
byIP[0] = (BYTE)(dwIPKey >> 24);
byIP[1] = (BYTE)(dwIPKey >> 16);
byIP[2] = (BYTE)(dwIPKey >> 8);
byIP[3] = (BYTE)(dwIPKey);
_snprintf( pszIP, nBufSize, "%d.%d.%d.%d", byIP[0], byIP[1], byIP[2], byIP[3]);
return TRUE;
}