[原創]Python 各進制間的轉換
最近在做協議分析時經常要用些進制間的轉換函數.
記錄存檔:








































#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))
posted on 2011-02-28 13:03 天下 閱讀(11265) 評論(0) 編輯 收藏 引用 所屬分類: Python