ip、數字的互轉

pythontab發表於2015-05-04

ip、數字互轉


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


相關文章