由数字转换到子网掩码

https://blog.csdn.net/u012258978/article/details/54022480

// testconv.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <cstring>
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
#include <string>
#define LMOVE(m,n) ((m)<<(n))
#define RMOVE(m,n) ((m)>>(n))

using namespace std;
#ifdef WIN32
#define snprintf _snprintf
#endif
static std::string convert(int n)
{
	char buf[32] = {0};
	unsigned int addr = LMOVE(0xffffffff,32 - n);
	snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
		RMOVE(addr,24), RMOVE(addr,16)&0xff, RMOVE(addr,8)&0xff, addr&0xff);
	return buf;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int pnum = 252;
	string str11 = convert(pnum);
	std::cout<<str11<<std::endl;
	system("PAUSE");
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/hshy/p/12824476.html