当前位置:网站首页>IP address to integer
IP address to integer
2022-06-24 19:36:00 【Potato, watermelon and sesame】
IP Address to integer : such as "192.168.1.254", Split by point , take 192 168 1 254 Save in a int a[4] In the array , And then through unsigned int nResult = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0] Get an unsigned 32 An integer .
Convert integer to IP Address : Convert this integer to an unsigned integer 32 Bit binary number . From left to right , Divide every eight bits , obtain 4 paragraph 8 The binary number of bits , Convert these binary numbers to integers
#include <iostream>
#include <string>
using namespace std;
unsigned int IPToValue(const string& strIP)//IP Convert to integer
{
//IP Convert to numeric
// No format check
// The return value is the result
int a[4];
string IP = strIP;
string strTemp;
size_t pos;
size_t i = 3;
do
{
pos = IP.find(".");
if (pos != string::npos)
{
strTemp = IP.substr(0, pos);
a[i] = atoi(strTemp.c_str());//int atoi(const char *str);
i--;
IP.erase(0, pos + 1);// From the position 0 To delete pos+1 Characters
}
else
{
strTemp = IP;
a[i] = atoi(strTemp.c_str());
break;
}
} while (1);
unsigned int nResult = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
return nResult;
}
string ValueToIP(const int& nValue)// Convert integer to ip
{
// The value is converted to IP
// No format check
// The return value is the result
char strTemp[20];
sprintf(strTemp, "%d.%d.%d.%d",
(nValue & 0xff000000) >> 24,
(nValue & 0x00ff0000) >> 16,
(nValue & 0x0000ff00) >> 8,
(nValue & 0x000000ff));
return string(strTemp);
}
int main()
{
string strIP = "192.168.1.254";
cout << IPToValue(strIP) << endl;
cout << ValueToIP(3232236030) << endl;
return 0;
}边栏推荐
- Introduction to smart contract security audit delegatecall (2)
- R for Data Science (note) -- data transformation (select basic use)
- Understanding openstack network
- PHP OSS file reads and writes files, and workman generates temporary files and outputs them to the browser for download
- Real time rendering: the difference between real-time, offline, cloud rendering and hybrid rendering
- 60 个神级 VS Code 插件!!
- Instruction rearrangement concept
- 模块五
- 8 challenges of BSS application cloud native deployment
- 敏捷之道 | 敏捷开发真的过时了么?
猜你喜欢

Drawing DEM with GEE gracefully

Technology implementation | Apache Doris cold and hot data storage (I)

The efficiency of okcc call center data operation

一文详解|Go 分布式链路追踪实现原理

Some small requirements for SQL Engine for domestic database manufacturers

starring V6平台开发接出点流程

工作6年,月薪3W,1名PM的奋斗史

多云模式并非“万能钥匙”

Camera module and hardware interface of Camera1 camera

three. Basic framework created by JS
随机推荐
Experience of MDM master data project implementation for manufacturing projects
制造业项目MDM主数据项目实施心得
程序员大部分时间不是写代码,而是。。。
Docker installing MySQL
[go Language brossage] go from 0 to Getting started 4: Advanced use of slice, Primary Review and Map Getting started Learning
假如,程序员面试的时候说真话
The efficiency of okcc call center data operation
AI时代生物隐私如何保护?马德里自治大学最新《生物特征识别中的隐私增强技术》综述,全面详述生物隐私增强技术
我链接mysql 报这个错 是啥意思呀?
Dataworks development ODPs SQL development production environment automatic completion of ProjectName
Digital twin industry case: Digital Smart port
ls 常用参数
Geoscience remote sensing data collection online
mysql binlog 数据源配置文档麻烦分享一下
Source code analysis of ArrayList
How to use R package ggtreeextra to draw evolution tree
Test drive citus 11.0 beta (official blog)
佛祖保佑 永无BUG
企业网络管理员必备的故障处理系统
优维低代码:构件渲染子构件