当前位置:网站首页>Conversion between HJ integer and IP address
Conversion between HJ integer and IP address
2022-06-28 07:38:00 【Courageous steak】
describe
principle :ip Each segment of the address can be regarded as a 0-255 The integer of , Split each segment into a binary form and put it together , Then convert this binary number into
A long integer .
give an example : One ip The address is 10.0.3.193
| Every number | Corresponding binary number |
|---|---|
| 10 | 00001010 |
| 0 | 00000000 |
| 3 | 00000011 |
| 193 | 11000001 |
The combination is :00001010 00000000 00000011 11000001, Convert to 10 The hexadecimal number is :167773121, That is, the IP The number after address conversion is it .
Data range : Ensure that the input is legal IP Sequence
Input description :
Input
1 Input IP Address
2 Input 10 Binary IP Address
Output description :
Output
1 Output to 10 It's binary IP Address
2 Output converted IP Address
Example 1
Input :
10.0.3.193
167969729
Output :
167773121
10.3.3.193
Cattle guest Python
def ip2num(ip):
res = ''
for i in ip.split('.'):
res = res + bin(int(i))[2:].rjust(8, "0")
return int('0b' + res, 2)
def num2ip(num):
result = ''
bin_str = bin(int(num))[2:]
bin_str = bin_str.rjust(32, '0')
res = ''
for item in range(4):
res = res + "." + str(int('0b' + bin_str[item * 8:item * 8 + 8], 2))
return res[1:]
if __name__ == '__main__':
# while True:
try:
print(ip2num(input()))
print(num2ip(input()))
# print(ip2num("10.0.3.193"))
# print(num2ip("167969729"))
except:
pass
Link to the original text :
https://www.920vip.net/article/178
边栏推荐
- Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
- Vivo browser rapid development platform practice - Overview
- Devtools implementation principle and performance analysis practice
- Co process, asyncio, asynchronous programming
- Real time database - Notes
- goland IDE和delve调试位于kubernetes集群中的go程序
- 东方财富上开户是安全的吗
- R language ggmap visual cluster
- Sword finger offer II 091 Paint the house
- Section VI UART of zynq
猜你喜欢

Disposition Flex

Flex layout

Design of DSP image data stream

linux下修改mysql端口号

安全培训是员工最大的福利!2022新员工入职安全培训全员篇

Porting ucosiii to stm32f429

8 张图 | 剖析 Eureka 的首次同步注册表

Section Xi. Axi of zynq_ Use of DMA

Section VI UART of zynq

In idea, the get and set methods may be popular because the Lombok plug-in is not installed
随机推荐
ACM笔记
云原生:云计算技术再次升级 开启全面云开发时代
es6箭头函数中return的用法
The practice of traffic and data isolation in vivo Reviews
阿里云服务器创建快照、回滚磁盘
PLC -- Notes
Mysql8.0 and mysql5.0 accessing JDBC connections
Kubernetes cluster lossless upgrade practice
MMR rearrangement (similarity is calculated by editing distance and repeatability)
ABAP skill tree
R language Kolmogorov Smirnov tests whether the two samples follow the same distribution.
HJ成绩排序
How to configure DDR3 of dm8148
Kubernetes cluster command line tool kubectl
Uninstall and reinstall the latest version of MySQL database. The test is valid
kubernetes集群命令行工具kubectl
代码提交规范
Jetpack - defects of livedata component and Countermeasures
"Three routines" of digital collection market
Unity UI shadow component