[python-win32] Re: hex(-1) at python 2.4

Michael Spencer mahs at telcopartners.com
Fri Apr 22 23:47:30 CEST 2005


Wang Charles wrote:
> under phython
> 
>>>>hex(-1)
> 
> '-0x1'
> 
> but i want a 2.3 style output like
> 
> 
>>>>hex(-1)
> 
> '0xffffffff'
> 
> what should i do?
The following:

  >>> def hex(x):
  ...     return "0x%x" % ((x < 0) * 0x100000000 + x)

should return the same output on 2.3/2.4

If you are concerned about non-32 bit systems, then you could write more 
conservatively:
  >>> import sys
  >>> WORD = (sys.maxint+1)*2
  >>> def hex(x):
  ...     return "0x%x" % ((x < 0) * WORD + x)
  ...

HTH
Michael







More information about the Python-win32 mailing list