ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> edu-sig
edu-sig
[Edu-sig] More re: Advanced or beginner?
by Kirby Urner other posts by this author
Aug 1 2001 3:09PM messages near this date
[Edu-sig] Advanced or beginner? | RE: [Edu-sig] More re: Advanced or beginner?
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


_______________________________________________
Edu-sig mailing list
Edu-sig@[...].org
http://mail.python.org/mailman/listinfo/edu-sig
Thread:
Kirby Urner
Kirby Urner
Tim Peters

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved