From: Lie Ryan on 7 Jun 2010 06:20 On 06/07/10 19:31, Richard Thomas wrote: > On Jun 7, 10:17 am, Peter Otten <__pete...(a)web.de> wrote: >> Alfred Bovin wrote: >>> I'm working on something where I need to read a (binary) file bit by bit >>> and do something depending on whether the bit is 0 or 1. >> >>> Any help on doing the actual file reading is appreciated. >> >> The logical unit in which files are written is the byte. You can split the >> bytes into 8 bits... >> >>>>> def bits(f): >> >> ... while True: >> ... b = f.read(1) >> ... if not b: break >> ... b = ord(b) >> ... for i in range(8): >> ... yield b & 1 >> ... b >>= 1 >> ...>>> with open("tmp.dat", "wb") as f: # create a file with some example data >> >> ... f.write(chr(0b11001010)+chr(0b10101111))>>> with open("tmp.dat", "rb") as f: >> >> ... for bit in bits(f): >> ... print bit >> ... >> 0 >> 1 >> 0 >> 1 >> 0 >> 0 >> 1 >> 1 >> 1 >> 1 >> 1 >> 1 >> 0 >> 1 >> 0 >> 1 >> >> but that's a very inefficient approach. If you explain what you are planning >> to do we can most certainly come up with a better alternative. >> >> Peter > > You're reading those bits backwards. You want to read the most > significant bit of each byte first... Of course that depends on the need of the OP?
From: Peter Otten on 7 Jun 2010 06:28 Richard Thomas wrote: > On Jun 7, 10:17 am, Peter Otten <__pete...(a)web.de> wrote: >> Alfred Bovin wrote: >> > I'm working on something where I need to read a (binary) file bit by >> > bit and do something depending on whether the bit is 0 or 1. >> >> > Any help on doing the actual file reading is appreciated. >> >> The logical unit in which files are written is the byte. You can split >> the bytes into 8 bits... >> >> >>> def bits(f): >> >> ... while True: >> ... b = f.read(1) >> ... if not b: break >> ... b = ord(b) >> ... for i in range(8): >> ... yield b & 1 >> ... b >>= 1 >> ...>>> with open("tmp.dat", "wb") as f: # create a file with some example >> data >> >> ... f.write(chr(0b11001010)+chr(0b10101111))>>> with open("tmp.dat", >> "rb") as f: >> >> ... for bit in bits(f): >> ... print bit >> ... >> 0 >> 1 >> 0 >> 1 >> 0 >> 0 >> 1 >> 1 >> 1 >> 1 >> 1 >> 1 >> 0 >> 1 >> 0 >> 1 >> >> but that's a very inefficient approach. If you explain what you are >> planning to do we can most certainly come up with a better alternative. >> >> Peter > > You're reading those bits backwards. You want to read the most > significant bit of each byte first... > > Richard. >>> def bits(f): .... while True: .... byte = f.read(1) .... if not byte: break .... byte = ord(byte) .... for i in reversed(range(8)): .... yield byte >> i & 1 .... >>> with open("tmp.dat", "rb") as f: .... for bit in bits(f): .... print bit, .... 1 1 0 0 1 0 1 0 1 0 1 0 1 1 1 1
From: Nobody on 7 Jun 2010 06:41 On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: > You're reading those bits backwards. You want to read the most > significant bit of each byte first... Says who? There is no universal standard for bit-order. Among bitmap image formats, XBM is LSB-first while BMP and PBM are MSB-first. OpenGL reads or writes bitmap data in either order, controlled by glPixelStorei(). Most serial communication links (e.g. RS-232, ethernet) transmit the LSB first, although there are exceptions (e.g. I2C uses MSB-first).
From: Ulrich Eckhardt on 7 Jun 2010 07:20 Nobody wrote: > On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: > >> You're reading those bits backwards. You want to read the most >> significant bit of each byte first... > > Says who? Says Python: >>> bin(192) '0x11000000' That said, I totally agree that there is no inherently right way and I guess Richard was just a smiley or two short in order to have correct markup in his not-so-serious posting. :^) Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Peter Otten on 7 Jun 2010 07:43 Ulrich Eckhardt wrote: > Nobody wrote: >> On Mon, 07 Jun 2010 02:31:08 -0700, Richard Thomas wrote: >> >>> You're reading those bits backwards. You want to read the most >>> significant bit of each byte first... >> >> Says who? > > Says Python: > >>>> bin(192) > '0x11000000' Hmm, if that's what /your/ Python says, here's mine to counter: >>> bin(192) '0_totally_faked_binary_00000011' ;) Peter
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Non Sequitur: Re: Python Forum Next: os.path.normpath question |