pack an integer into a string

Paul Rubin http
Fri Jul 24 21:30:46 EDT 2009


superpollo <user at example.net> writes:
>   >>> number = 252509952
>   >>> hex(number)
>   '0xf0cff00'
>   >>>
> 
> so i would like a string like '\xf0\xcf\xf0\x00'

def encode(number):
   h = '%x' % number
   if len(h) % 2 == 1:
     h = '0' + h 
   return h.decode('hex')



More information about the Python-list mailing list