bit manipulation frustration

Mike Fletcher mfletch at tpresence.com
Sun Jul 23 00:59:01 EDT 2000


(This is untested (don't have data file), and hacky, but should give ideas
for a one-off):

input = open('somefile.dat').read()
file = []
currentlength = 4
while input:
	pad = '\000'*( (88-currentlength)/2)
	line = pad + input[:currentlength] + pad
	input = input[currentlength:]
	if len(file) > 21:
		currenlength = currentlength - 4
	else:
		currenlength = currentlength + 4

Oh, for converting to 16-bit integers (unsigned)
import struct
def convert( line ):
	return struct.unpack ( 'H'*(len(line)/2), line )

(That's in machine-native order, use '>'+('H'*...) for network order).

HTH,
Mike

-----Original Message-----
From: Courageous [mailto:jkraska1 at san.rr.com]
Sent: Saturday, July 22, 2000 11:39 PM
To: python-list at python.org
Subject: bit manipulation frustration



Okay. I'm getting a bit frustrated here. I'm trying to suck
bits out of a file (which is in a simple, but non standard
image format) and then convert this to a bitmap for wxPython.
I haven't gotten to the wxBitmap part yet, which I am assuming
will be easy, but the in-between part is driving me nuts.

First, in one fell swoop of antigenious, I seem to have
forgotten how to convert strings to sequences of char and
vice versa. Eeek. Help? Will someone please remind me and
hit me with a rubber mallet??? :)

Second, this whole paradigm seems so skrewball. While I
know exactly how to implement all of this in a external
C module, the program I'm writing is just a one off, and
I was expecting it to be easier in python.

The bitmap image is 16 bit color, but it's stored in
byte runs. As I load the image, I need to load 2,4,6,
8...44,42,40...2, 16 bit chunks at a time, but as I'm
doing this I have to create "black" runs on either side,
making the whole thing 44x44 (it's an isometric tile...
a square rotated 45 degrees, but bitmaps are square,
get it?)

I'm not seeing an elegant solution here; loading the
image as a string to begin with isn't right, because
my chunks are actually two byte chunks.

Anyone with any practical experience on how to *BEST*
do this kind of thing?

Am I just not thinking about this right?




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




More information about the Python-list mailing list