Jimmie He <jimmie...@gmail.com> wrote: >When I run the readbmp on an example.bmp(about 100k),the Shell is become to >"No respose",when I change f.read() to f.read(1000),it is ok,could someone >tell me the excat reason for this? >Thank you in advance! > >Python Code as below!! > >import binascii > >def read_bmp(): > f = open('example.bmp','rb') > rawdata = f.read() #f.read(1000) is ok > hexstr = binascii.b2a_hex(rawdata) #Get an HEX number > bsstr = bin (int(hexstr,16))[2:]
I suspect the root of the problem here is that you don't understand what this is actually doing. You should run this code in the command-line interpreter, one line at a time, and print the results. The "read" instruction produces a string with 100k bytes. The b2a_hex then produces a string with 200k bytes. Then, int(hexstr,16) takes that 200,000 byte hex string and converts it to an integer, roughly equal to 10 to the 240,000 power, a number with some 240,000 decimal digits. You then convert that integer to a binary string. That string will contain 800,000 bytes. You then drop the first two characters and print the other 799,998 bytes, each of which will be either '0' or '1'. I am absolutely, positively convinced that's not what you wanted to do. What point is there in printing out the binary equavalent of a bitmap? Even if you did, it would be much quicker for you to do the conversion one byte at a time, completely skipping the conversion to hex and then the creation of a massive multi-precision number. Example: f = open('example.bmp','rb') rawdata = f.read() bsstr = [] for b in rawdata: bsstr.append( bin(ord(b)) ) bsstr = ''.join(bsstr) or even: f = open('example.bmp','rb') bsstr = ''.join( bin(ord(b))[2:] for b in f.read() ) -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list