# -*- coding: utf-8 -*- """ dec to hex """ print hex(267) #0x10b print hex(10) #0xa print hex(40) #0x28 """ hex to dec """ print int('ff', 16) #255 """ char to dec """ print ord('a') #97 """ char to hex """ print hex(ord('a')) #0x61 """ hex to char """ print ord("\x61") #97 print chr(ord("\x61")) #a """ formatted hex with leading 0 """ print hex(10) #0xa print "%#04x" % 10 #0x0a """ formatted hex from char """ print "%#04x" % ord('a') #0x61 print "%#04x" % ord('\n') #0x0a