Home  >  Article  >  Backend Development  >  IP and digital conversion

IP and digital conversion

大家讲道理
大家讲道理Original
2016-11-07 17:30:461249browse

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')

3232235521

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

'192.168.0.1'

Statement:
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