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<strlen(szIP); i++)
	{
		for(int j=0; j<4; j++)
		{
			szBinIP[k++] = (mask & (szIP[i]-48)) ? 49:48;

			mask >>= 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值