当前位置:网站首页>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
边栏推荐
- Evaluation of inverse Polish expression < difficulty coefficient >
- Cloud native (to be updated)
- 同花顺网上开户安全吗
- 分析 NFT 项目的 5 个指标
- A gadget can write crawlers faster
- linux下修改mysql用户名root
- R 语言 Hitters 数据分析
- Solving the longest palindrome substring by dynamic programming
- Section 9: dual core startup of zynq
- R language drawing ggplot2 seasonal graph
猜你喜欢

Kubernetes theoretical basis

Source code analysis of kubernetes' process of deleting pod

2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached

Leetcode+ 51 - 55 retrospective and dynamic planning topics

kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑

The practice of event driven architecture in vivo content platform

Evolution of vivo push platform architecture

以动态规划的方式求解最长回文子串

linux下修改mysql端口号

Leetcode learning records
随机推荐
Redis one master multi slave cluster setup
es6箭头函数中return的用法
R 和 rgl 绘制 3D 结
Is it safe to open an account on Dongfang fortune
linux下修改mysql端口号
Solving the longest palindrome substring by dynamic programming
Is it safe for flush to open an account online
No suspense about the No. 1 Internet company overtime table
NDK cross compilation
ACM notes
Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers
Ice, protobuf, thrift -- Notes
QT -- communication protocol
Section VII starting principle and configuration of zynq
Ice - resources
Hash slot of rediscluster cluster cluster implementation principle
Section VI UART of zynq
flutter 实现摇一摇功能
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
Force buckle 515 Find the maximum value in each tree row