当前位置:网站首页>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
边栏推荐
- Mysql8.0 and mysql5.0 accessing JDBC connections
- linux下修改mysql用户名root
- Kubernetes cluster command line tool kubectl
- Sword finger offer|: linked list (simple)
- HJ进制转换
- Solving the longest palindrome substring by dynamic programming
- R 语言 ggmap
- ES6 use of return in arrow function
- Hack the box:routerspace
- Kubernetes理论基础
猜你喜欢

linux下修改mysql用户名root

A gadget can write crawlers faster

Analyze 5 indicators of NFT project

Cloud native (to be updated)

ABAP 技能树

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

Modifying MySQL port number under Linux

Section VII starting principle and configuration of zynq

Principle and practice of bytecode reference detection
![[ thanos源码分析系列 ]thanos query组件源码简析](/img/e4/2a87ef0d5cee0cc1c1e1b91b6fd4af.png)
[ thanos源码分析系列 ]thanos query组件源码简析
随机推荐
7-2 Finnish wooden chess structure Sorting
okcc呼叫中心没有电脑的坐席能不能开展工作?
QT -- communication protocol
ACM notes
HJ明明的随机数
云原生(待更新)
Installing redis on Linux
Sword finger offer|: linked list (simple)
A gadget can write crawlers faster
PLC -- Notes
数字藏品市场“三大套路”
R语言绘制 ggplot2 季节性图
Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
Uninstall and reinstall the latest version of MySQL database. The test is valid
Path alias specified in vite2.9
Llvm and clang
Vivo browser rapid development platform practice - Overview
Section Xi. Axi of zynq_ Use of DMA
Unity-UI-shadow组件