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

2008-08-26 Thread Alan Gauld
"Py Hex" <[EMAIL PROTECTED]> wrote 


type(hex(12))



I get a string type back, i.e, '0xC' not 0xC


Thats right because hex is a representation format.
The data (12 in this case) is stored in binary on the PC.
What you see as hex (or decimal or ocatal...) is just 
a string representation of the underlying binary data.


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)




Close. Python understands the string of characters you 
typed is in hex format and interprets the string as an integer 
value which it stores in binary internally.


How can I convert this string returned by hex builtin 
function to data with 0x prefixed ? 


The 0x prefix is a string. The data is always in binary.
You cannot convert a number to anything (other than 
int-float etc) you can only change its string representation.


Am I missing anything ? 


Yes, you are missing the underlying data representation 
within the machine. You need to distinguish clearly in your 
mind the difference between what is stored and what is 
displayed. The storage is always binary. The display is 
whatever format you ask for (with decimal as default)


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2008-08-25 Thread Kent Johnson
On Mon, Aug 25, 2008 at 8:45 PM, Py Hex <[EMAIL PROTECTED]> wrote:
> When I run this:
>
 type(hex(12))
> 
>
> 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)
> 
>
> Why does the Hex builtin function in Python return a string ?  How can I 
> convert this string returned by hex builtin function to data with 0x prefixed 
> ?

I think you are confusing representation with value. 0xCD and 205 are
different representations of the same value. The interpreter
understands both but the values are the same. In fact if you ask
Python for the value of e in your example, it will say it is 205.

The base (10 or 16) is a property of the representation, not the
value. The hex() function gives you the string representation of the
value as a (base 16) hex string. The str() function gives the standard
(base 10) string representation of an integer.

Are you just curious or is there something specific you are trying to do?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2008-08-25 Thread John Fouhy
2008/8/26 John Fouhy <[EMAIL PROTECTED]>:
> The hex() function (and oct() too) provides you with a different
> string representation from the default.  If you want to change python
> to display integers in hex instead of decimal by default, I can't help
> you.. (well, maybe you could subclass int, and change __repr__ and
> __str__ to return hex strings)

Actually, that was easier than I thought:

class HexInt(int):
def __repr__(self):
return hex(self)
def __str__(self):
return str(self)
def __add__(self, other):
return HexInt(int(self)+int(other))
def __sub__(self, other):
return HexInt(int(self)-int(other))
def __mul__(self, other):
return HexInt(int(self)*int(other))
def __div__(self, other):
return HexInt(int(self)/int(other))

>>> h1 = HexInt(13)
>>> h2 = HexInt(21)
>>> h1, h2
(0xd, 0x15)
>>> h1+h2
0x22
>>> h1-h2
-0x8
>>> h1*h2
0x111
>>> int(h1*h2)
273
>>> h1+16
0x1d

Of course, there's obvious problems if you want to mix this with
floats :-/  And I'm not sure what you'd gain, since as mentioned,
integers are integers, whatever they look like.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2008-08-25 Thread John Fouhy
2008/8/26 Py Hex <[EMAIL PROTECTED]>:
> When I run this:
>
 type(hex(12))
> 
>
> 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)
> 
>
> Why does the Hex builtin function in Python return a string ?  How can I 
> convert this string returned by hex builtin function to data with 0x prefixed 
> ?

If you type 0xC, you get a number.  Try it:

>>> 0xC
12

'12' is the default python representation of the integer 0xC.
Internally, it is (I guess) stored as a 4 byte chunk of memory; that
is, a 32-bit long binary.  There is _no difference_ between 0xC and
12:

>>> 0xC is 12
True

The hex() function (and oct() too) provides you with a different
string representation from the default.  If you want to change python
to display integers in hex instead of decimal by default, I can't help
you.. (well, maybe you could subclass int, and change __repr__ and
__str__ to return hex strings)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor