char型IP和2进制IP互转

int WriteConfig(HWND hwnd)
{
	
	HWND hwndIP;
	ofstream outFile;
	char szFilePath[MAX_PATH] = "\0";   //配置文件路径
	char szFileName[MAX_PATH] = "ini.cfg";  //文件名
	char *pFile;
	char szIP[MAX_PATH] = "\0";  // 保存char型IP
	char szBinIP[MAX_PATH] = "\0"; //保存2进制IP
	int  k=0, mask=8;
	int iLength;

	//取得程序路径
	GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
	pFile = strrchr(szFilePath, '\\');
	*(++pFile) = '\0';
	strcat(szFilePath, szFileName);
	
	//取得IP地址
	hwndIP = GetDlgItem(hwnd, IDE_IPEDIT);
	iLength = GetWindowTextLength(hwndIP);
	GetWindowTextA(hwndIP, szIP, iLength+1);

	//获得checkbox标志
	if (IsDlgButtonChecked(hwnd, IDC_AUTORUN) == BST_CHECKED)
	{
		strcat(szIP, "-1");
	}

	//把char型IP转换为2进制,每次只抓换一个数字
	for (short i=0; i>= 1;
		}

		mask = 8;  
	}

	OutputDebugStringA(szBinIP);

	//写入配置
	outFile.open(szFilePath, ios_base::out | ios_base::binary);
	outFile.write(szBinIP, lstrlenA(szBinIP));
	outFile.close();

	return 0;
}
int ReadConfig(HWND hwnd)
{

	HWND hwndIP;
	ifstream inFile;
	char szFilePath[MAX_PATH] = "\0";  //配置文件路径
	char szFileName[MAX_PATH] = "ini.cfg";  //名字
	char *pFile;
	char szIP[MAX_PATH] = "\0";  // 保存读取的char型IP
	char szBinaryIP[MAX_PATH] = "\0";  //保存2进制的IP
	int iIP = 0; 
	unsigned int len;
	char temp[MAX_PATH] = "\0";
	char *pMask;
	
	//取得程序路径
	GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
	pFile = strrchr(szFilePath, '\\');
	*(++pFile) = '\0';
	strcat(szFilePath, szFileName);
	//读取配置文件
	inFile.open(szFilePath, ios_base::in | ios_base::binary);
	inFile.seekg(ios::beg);
	inFile.read(szIP, 4);
	//循环读取配置文件,每次读4个2进制字节,在转换
	while (inFile.good()) 
	{
		len = strlen(szIP) - 1;
		iIP = 0;

		for (int i = 0; i <= len; i++)
		{		
			iIP += (szIP[i] - '0') * (1 << (len - i));
		}
		// 排除配置文件里的符号
		if (iIP == 14)
		{
			strcat(szBinaryIP, ".");
		}
		else if (iIP == 13)  
		{
			strcat(szBinaryIP, "-");
		}
		else if (iIP == 12)
		{
			strcat(szBinaryIP, ",");
		}
		else
		{
			sprintf(temp, "%d", iIP);
			strcat(szBinaryIP, temp);
		}
		inFile.read(szIP, 4);
	}
	inFile.close();
	//查找并设置checkbox的标志
	if (strstr(szBinaryIP, "-1") != NULL)
	{
		if (CheckAutoRun())
		{
			CheckDlgButton(hwnd, IDC_AUTORUN, BST_CHECKED);
		}
		pMask = strchr(szBinaryIP, '-');
		*pMask = '\0';
	}

	//设置IP地址
	hwndIP = GetDlgItem(hwnd, IDE_IPEDIT);
	SetWindowTextA(hwndIP, szBinaryIP);

	return 0;
}


你可能感兴趣的:(C/C++)