Home > Backend Development > Python Tutorial > IP and digital conversion

IP and digital conversion

大家讲道理
Release: 2016-11-07 17:30:46
Original
1283 people have browsed it

ip, digital transfer

# ip ==> 数字
>>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])])
>>> ip2num('192.168.0.1')
Copy after login

3232235521

# 数字 ==> ip # 数字范围[0, 255^4]
>>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)])
>>> num2ip(3232235521)
Copy after login

'192.168.0.1'

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template