Integer as raw hex string?

2012-12-24 Thread Roy Smith
I have an integer that I want to encode as a hex string, but I don't 
want 0x at the beginning, nor do I want L at the end if it happened 
to be a long.  The result needs to be something I can pass to int(h, 16) 
to get back my original integer.

The brute force way works:

   h = hex(i)
   assert h.startswith('0x')
   h = h[2:]
   if h.endswith('L'):
   h = h[:-1]

but I'm wondering if there's some built-in call which gives me what I 
want directly.  Python 2.7.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Integer as raw hex string?

2012-12-24 Thread Tim Chase
On 12/24/12 09:36, Roy Smith wrote:
 I have an integer that I want to encode as a hex string, but I don't 
 want 0x at the beginning, nor do I want L at the end if it happened 
 to be a long.  The result needs to be something I can pass to int(h, 16) 
 to get back my original integer.
 
 The brute force way works:
 
h = hex(i)
assert h.startswith('0x')
h = h[2:]
if h.endswith('L'):
h = h[:-1]
 
 but I'm wondering if there's some built-in call which gives me what I 
 want directly.  Python 2.7.

Would something like

  h = %08x % i

or

  h = %x % i

work for you?

-tkc


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Integer as raw hex string?

2012-12-24 Thread Roy Smith
In article mailman.1256.1356364625.29569.python-l...@python.org,
 Tim Chase python.l...@tim.thechases.com wrote:

 On 12/24/12 09:36, Roy Smith wrote:
  I have an integer that I want to encode as a hex string, but I don't 
  want 0x at the beginning, nor do I want L at the end if it happened 
  to be a long.  The result needs to be something I can pass to int(h, 16) 
  to get back my original integer.
  
  The brute force way works:
  
 h = hex(i)
 assert h.startswith('0x')
 h = h[2:]
 if h.endswith('L'):
 h = h[:-1]
  
  but I'm wondering if there's some built-in call which gives me what I 
  want directly.  Python 2.7.
 
 Would something like
 
   h = %08x % i
 
 or
 
   h = %x % i
 
 work for you?

Duh.  Of course.  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Integer as raw hex string?

2012-12-24 Thread MRAB

On 2012-12-24 15:58, Tim Chase wrote:

On 12/24/12 09:36, Roy Smith wrote:

I have an integer that I want to encode as a hex string, but I don't
want 0x at the beginning, nor do I want L at the end if it happened
to be a long.  The result needs to be something I can pass to int(h, 16)
to get back my original integer.

The brute force way works:

   h = hex(i)
   assert h.startswith('0x')
   h = h[2:]
   if h.endswith('L'):
   h = h[:-1]

but I'm wondering if there's some built-in call which gives me what I
want directly.  Python 2.7.


Would something like

   h = %08x % i

or

   h = %x % i

work for you?


Or:

h = {:x}.format(i)

--
http://mail.python.org/mailman/listinfo/python-list