Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41124 > unrolled thread

Re: Reversing bits in a byte

Started byTim Chase <python.list@tim.thechases.com>
First post2013-03-12 08:20 -0500
Last post2013-03-12 08:20 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Reversing bits in a byte Tim Chase <python.list@tim.thechases.com> - 2013-03-12 08:20 -0500

#41124 — Re: Reversing bits in a byte

FromTim Chase <python.list@tim.thechases.com>
Date2013-03-12 08:20 -0500
SubjectRe: Reversing bits in a byte
Message-ID<mailman.3231.1363097544.2939.python-list@python.org>
On 2013-03-11 15:32, Robert Flintham wrote:
> I have a 'bytes' object which contains a simple bitmap image (i.e.
> 1 bit per pixel).  I can't work out how I would go about displaying
> this image.  Does anyone have any thoughts?

You'd need to detail
- how you want to display it (console, GUI, web page)
- how you know what the dimensions are
- the bit order

It could be something as simple as

  HEIGHT = 40
  some_bytes = file('data.bin').read()
  WIDTH = len(some_bytes) // HEIGHT
  for i, byte in enumerate(some_bytes):
    if i and i % WIDTH == 0:
      print # a new line
    for bit in range(8):
      if byte & (1 << bit):
        print '*',
      else:
        print ' ',


-tkc

> DISCLAIMER:
[trim a paragraph of useless junk]
Please remove these disclaimers if at all possible.  You're posting
to a public forum, which pretty much waives all credibility to the
disclaimer (not that they've held much legal standing in any argument
I've heard).




[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web