[Tutor] Fwd: Why does the Hex builtin function in Python return a string ?

W W srilyk at gmail.com
Tue Aug 26 03:13:50 CEST 2008


Forgot to send to list...

On Mon, Aug 25, 2008 at 7:45 PM, Py Hex <pyhex at yahoo.com> wrote:

> When I run this:
>
> >>> type(hex(12))
> <type 'str'>
>
> I get a string type back, i.e, '0xC' not 0xC
>
> On the other hand, if I use 0x with data, Python understands it is hex data
> and not a string value.
>
> >>> e = 0xCD
> >>> type(e)
> <type 'int'>


You missed trying something:

>>> e = 0xCD
>>> e
205
>>> type(e)
<type 'int'>

It doesn't store the value as hex data, it stores it as an integer.

I'm really not sure about anything else (i.e. converting the value to an
integer - I've tried and int(e) doesn't work when it's a hex string) though.

HTH, Wayne

p.s. After a quick Google, I discovered how to convert the other way:
int('0xCD', 0) will give you the integer value of your string (if it's hex.
If you're doing octal you'll want int(myOctal, 8) )

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080825/56b416d2/attachment.htm>


More information about the Tutor mailing list