Binary Input: read how?

Mike Fletcher mfletch at tpresence.com
Mon Jun 19 19:04:24 EDT 2000


You're currently trying to & a string, only numbers respond to &'ing in
Python.  ord(b[0]) gives you the integer value for the character.

>>> for x in range(30):
... 	if ord(b[0]) & (2**x):
... 		print 1,
... 	else:
... 		print 0,
... 
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

See also various recent postings about getting the "bits" of a number
(though you'll still need to do an ord on the character).

HTH,
Mike

-----Original Message-----
From: Michael Morrison [mailto:borlak at home.com]
Sent: Monday, June 19, 2000 6:49 PM
To: python-list at python.org
Subject: Binary Input: read how?


Here's the basic problem: I can read a binary file using open(...,'rb') but
when I assign it to a object (b = file.read()) it stores it as an escape
character or \300 - whatever range.

I want to test this byte of binary data by comparing it's bits.  b[0] & 1 ..
b[0] & 2
for x in range(30):
    if b[0] & 2**x:
        print 1,
    else
        print 0,

This doesn't work.  Tried all sorts of things.... Can't get it to work :(

What am I missing here?

thanks


-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list