Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35460 > unrolled thread
| Started by | Roy Smith <roy@panix.com> |
|---|---|
| First post | 2012-12-24 10:36 -0500 |
| Last post | 2012-12-24 18:15 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Integer as raw hex string? Roy Smith <roy@panix.com> - 2012-12-24 10:36 -0500
Re: Integer as raw hex string? Tim Chase <python.list@tim.thechases.com> - 2012-12-24 09:58 -0600
Re: Integer as raw hex string? Roy Smith <roy@panix.com> - 2012-12-24 11:21 -0500
Re: Integer as raw hex string? MRAB <python@mrabarnett.plus.com> - 2012-12-24 18:15 +0000
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-12-24 10:36 -0500 |
| Subject | Integer as raw hex string? |
| Message-ID | <roy-88A3B9.10360324122012@news.panix.com> |
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.
[toc] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-12-24 09:58 -0600 |
| Message-ID | <mailman.1256.1356364625.29569.python-list@python.org> |
| In reply to | #35460 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-12-24 11:21 -0500 |
| Message-ID | <roy-354E8C.11211524122012@news.panix.com> |
| In reply to | #35462 |
In article <mailman.1256.1356364625.29569.python-list@python.org>,
Tim Chase <python.list@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.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-12-24 18:15 +0000 |
| Message-ID | <mailman.1263.1356372951.29569.python-list@python.org> |
| In reply to | #35460 |
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)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web