[Edu-sig] More re: Advanced or beginner?

Kirby Urner pdx4d@teleport.com
Wed, 01 Aug 2001 08:09:21 -0700


Another way to look at the relationship between
bytes, integers, characters:

   >>> [hex(i) for i in array.array('b',"THIS")]
   ['0x54', '0x48', '0x49', '0x53']
    >>> binascii.hexlify("THIS")
   '54484953'
   >>> eval("0x"+binascii.hexlify("THIS"))
   1414023507
   >>> struct.pack('i',1414023507)
   'SIHT'

hexlify strings together hex bytes.  Four bytes will be
in reverse order from the 'i' type, such that packing
the resulting decimal will result in a four-byte string
in reverse order.  'h' works on byte pairs:

   >>> binascii.hexlify("MY")
   '4d59'
   >>> [hex(i) for i in array.array('b',"MY")]
   ['0x4d', '0x59']
   >>> eval("0x"+binascii.hexlify("MY"))
   19801
   >>> struct.pack('h',19801)
   'YM'

You may also be able to go "double-wide" with 8-byte
mappings of characters to double long floats (type d):

   >>> array.array('d',"ABCDEFHI")
   array('d', [1.0826786300000142e+045])
   >>> struct.pack('d',1.0826786300000142e+045)
   'ABCDEFHI'

Question:

Why does float -> long work like this:

    >>> long(1.0826786300000142e+045)
    1082678630000014218353234260713996413124476928L

and not like this?

    >>> long(1.0826786300000142e+045)
    1082678630000014200000000000000000000000000000L

Kirby