当前位置:网站首页>HJ整数与IP地址间的转换
HJ整数与IP地址间的转换
2022-06-28 07:36:00 【有勇气的牛排】
描述
原理:ip地址的每段可以看成是一个0-255的整数,把每段拆分成一个二进制形式组合起来,然后把这个二进制数转变成
一个长整数。
举例:一个ip地址为10.0.3.193
| 每段数字 | 相对应的二进制数 |
|---|---|
| 10 | 00001010 |
| 0 | 00000000 |
| 3 | 00000011 |
| 193 | 11000001 |
组合起来即为:00001010 00000000 00000011 11000001,转换为10进制数就是:167773121,即该IP地址转换后的数字就是它了。
数据范围:保证输入的是合法的 IP 序列
输入描述:
输入
1 输入IP地址
2 输入10进制型的IP地址
输出描述:
输出
1 输出转换成10进制的IP地址
2 输出转换后的IP地址
示例1
输入:
10.0.3.193
167969729
输出:
167773121
10.3.3.193
牛客 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
边栏推荐
- 7-1 understand everything
- ABAP skill tree
- Install haproxy
- GoLand IDE and delve debug Go programs in kubernetes cluster
- QT -- communication protocol
- Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
- The practice of traffic and data isolation in vivo Reviews
- Unity UI shadow component
- ice - 资源
- Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
猜你喜欢

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

Design and practice of vivo sensitive word matching system

A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages

Yesterday, I went to a large factory for an interview and asked me to do four arithmetic operations. Fortunately, I am smart enough

阿里云服务器创建快照、回滚磁盘

HTTP Caching Protocol practice

Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio

Unity-UI-shadow组件

Cloud native (to be updated)

Uninstall and reinstall the latest version of MySQL database. The test is valid
随机推荐
同花顺网上开户安全吗
[thanos source code analysis series]thanos query component source code analysis
R language ggmap visual cluster
linux下修改mysql用户名root
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
Rediscluster cluster mode capacity expansion node
Implementation of commit message standardized control in large projects
Introduction and several months' experience of extending the solution thanos of Prometheus
Design and implementation of spark offline development framework
es6箭头函数中return的用法
安全培训是员工最大的福利!2022新员工入职安全培训全员篇
mysql57 zip文件安装
kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
An important term in MySQL -- CRUD
Source code analysis of kubernetes' process of deleting pod
What is the lifecycle of automated testing?
vite2.9 中指定路径别名
Sword finger offer II 091 Paint the house
golang gin框架进行分块传输
Makefile