# 题解：整数与IP地址间的转换(Conversion between integer and IP address)-其他

## 题解：整数与IP地址间的转换(Conversion between integer and IP address)

10 00001010
0 00000000
3 00000011
193 11000001

1 输入IP地址
2 输入10进制型的IP地址

1 输出转换成10进制的IP地址
2 输出转换后的IP地址

``````输入：
10.0.3.193
167969729

167773121
10.3.3.193
``````
``````#include<iostream>
using namespace std;
int main()
{
long long int a,b,c,d;
long long int num;
scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
cin>>num;
cout<<(a<<24)+(b<<16)+(c<<8)+d<<endl;
a=num>>24;
num-=(a<<24);
b=num>>16;
num-=(b<<16);
c=num>>8;
num-=(c<<8);
d=num;
cout<<a<<'.'<<b<<"."<<c<<'.'<<d<<endl;
}

``````
————————

describe
Principle: each segment of IP address can be regarded as an integer from 0 to 255. Each segment is divided into a binary form and combined, and then the binary number is transformed into
A long integer.
Example: an IP address is 10.0.3.193
Binary number corresponding to each segment
10 00001010
0 00000000
3 00000011
193 11000001

The combination is: 0000100 00000000 00000011 11000001, which is converted to hexadecimal number: 167773121, that is, the converted number of the IP address is it.

Data range: ensure that the input is a legal IP sequence
Enter Description:
input

Output Description:
output
2 output the converted IP address

``````输入：
10.0.3.193
167969729

167773121
10.3.3.193
``````
``````#include<iostream>
using namespace std;
int main()
{
long long int a,b,c,d;
long long int num;
scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
cin>>num;
cout<<(a<<24)+(b<<16)+(c<<8)+d<<endl;
a=num>>24;
num-=(a<<24);
b=num>>16;
num-=(b<<16);
c=num>>8;
num-=(c<<8);
d=num;
cout<<a<<'.'<<b<<"."<<c<<'.'<<d<<endl;
}

``````