最近在做协议分析时经常要用些进制间的转换函数.
记录存档:








































#
IP地址之间的转换
import socket
import time
import struct
def ip2hex (ip):
return hex(struct.unpack( " !I " , socket.inet_aton(ip))[0])
def ip2long (ip):
return struct.unpack( " !I " , socket.inet_aton(ip))[0]
def long2ip (lint):
return socket.inet_ntoa(struct.pack( " !I " , lint))
#时间戳格式化
def l2t (lint):
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(lint))
import socket
import time
import struct
def ip2hex (ip):
return hex(struct.unpack( " !I " , socket.inet_aton(ip))[0])
def ip2long (ip):
return struct.unpack( " !I " , socket.inet_aton(ip))[0]
def long2ip (lint):
return socket.inet_ntoa(struct.pack( " !I " , lint))
#时间戳格式化
def l2t (lint):
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(lint))