当前位置:网站首页>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;
}边栏推荐
- 目前是不是只cdc 监控mysql 可以拿到新增列的数据 sqlserver不行是吧
- 假如,程序员面试的时候说真话
- Necessary fault handling system for enterprise network administrator
- Instruction rearrangement concept
- Fabric 账本数据块结构解析(一):如何解析账本中的智能合约交易数据
- Fabric ledger data block structure analysis (I): how to analyze the smart contract transaction data in the ledger
- Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node
- 我用sql形式的会出现cdc读取乱序吗
- 一文详解|Go 分布式链路追踪实现原理
- Multi cloud mode is not a "master key"
猜你喜欢

60 个神级 VS Code 插件!!

Generate the last login user account report of the computer through SCCM SQL

Game between apifox and other interface development tools

How to protect biological privacy in the AI era? Overview of the latest "privacy enhancement technology in biometrics" of the Autonomous University of Madrid, comprehensively detailing the biometric p

应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案

Based on STM32F103 0.96 inch OLED LCD driver (IIC communication)

Full link service tracking implementation scheme

Working for 6 years with a monthly salary of 3W and a history of striving for one PM

Power efficiency test

Download steps of STM32 firmware library
随机推荐
Xiaodi class massive data processing business short chain platform
请教一个问题。adbhi支持保留一个ID最新100条数据库,类似这样的操作吗
How to customize cursor position in wechat applet rotation chart
【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
特尔携手微软发挥边云协同势能,推动AI规模化部署
R语言 4.1.0软件安装包和安装教程
ls 常用参数
Redis error: -bash: redis cli: command not found
three. Basic framework created by JS
Instruction rearrangement concept
企业网络管理员必备的故障处理系统
Application scenarios of channel of go question bank · 11
Dataworks development ODPs SQL development production environment automatic completion of ProjectName
Volcano becomes spark default batch scheduler
NFT双币质押流动性挖矿系统开发
Experience of MDM master data project implementation for manufacturing projects
AI时代生物隐私如何保护?马德里自治大学最新《生物特征识别中的隐私增强技术》综述,全面详述生物隐私增强技术
佛祖保佑 永无BUG
目前是不是只cdc 监控mysql 可以拿到新增列的数据 sqlserver不行是吧
Network security review office starts network security review on HowNet