当前位置:网站首页>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;
}边栏推荐
- Kubernetes集群部署
- 通过SCCM SQL生成计算机上一次登录用户账户报告
- How to select the ECS type and what to consider?
- flink cdc全量读mysql老是报这个错怎么处理
- What type of datetime in the CDC SQL table should be replaced
- Why is the executor thread pool framework introduced
- 60 个神级 VS Code 插件!!
- The agile way? Is agile development really out of date?
- Fabric 账本数据块结构解析(一):如何解析账本中的智能合约交易数据
- Volcano成Spark默认batch调度器
猜你喜欢

Network security review office starts network security review on HowNet

Programmers spend most of their time not writing code, but...

Interprétation de la thèse (SR - gnn) Shift Robust GNNS: Overcoming the Limits of Localized Graph Training Data

Digital twin industry case: Digital Smart port

R语言corrplot相关热图美化实例分析

Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!

SaltStack State状态文件配置实例

How to use R package ggtreeextra to draw evolution tree

Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data

Freeswitch uses origin to dialplan
随机推荐
Mq-2 smoke concentration sensor (STM32F103)
Docker installing MySQL
Volcano becomes spark default batch scheduler
Volcano becomes spark default batch scheduler
At present, only CDC monitors Mysql to get the data of new columns. Sqlserver can't, can it
STM32 uses time delay to realize breathing lamp register version
R language 4.1.0 software installation package and installation tutorial
Unityshader world coordinates do not change with the model
Server lease error in Hong Kong may lead to serious consequences
ls 常用参数
How to use JWT authentication in thinkphp6
假如,程序员面试的时候说真话
Multi segment curve temperature control FB (SCL program) of PLC function block series
How to use R package ggtreeextra to draw evolution tree
一文理解OpenStack网络
thinkphp6中怎么使用jwt认证
Ask a question. Adbhi supports the retention of 100 databases with the latest IDs. Is this an operation like this
Dataworks development ODPs SQL development production environment automatic completion of ProjectName
工作6年,月薪3W,1名PM的奋斗史
想问下 pgsql cdc 账号同一个 多个 task 会有影响吗,我现在3个task 只有一个 有