Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Convert to Integer in Python

Name: Anonymous 2008-06-10 14:03


def readBMP( filename):
    f = file( filename)
    content = f.read()
    f.close()
    return content
   
def getDimensions( bytes):
    return bytes[18:22],bytes[22:26]

w,h = getDimensions( getBMP( 'test.bmp'))


how do i convert w and h to int?
atm (w,h) == ('\x05\x00\x00\x00', '\x05\x00\x00\x00')

Name: Anonymous 2012-05-26 7:45

>>22
Not portable.

static int fourchar2int(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3)
{
        if (b3 & 0x80) {
                return (~b3 & 0xff) * -16777216
                     + (~b2 & 0xff) * -65536
                     + (~b1 & 0xff) * -256
                     + (~b0 & 0xff) * -1
                     - 1;
        } else {
                return b3 * 16777216
                     + b2 * 65536
                     + b1 * 256
                     + b0 * 1;
        }
}

...

 struct {
  int a;
  int b;
 } d;
 unsigned char buf[8];
 ...
 fread(&buf, 1, sizeof(buf), pfile);
 d.a = fourchar2int(buf[0], buf[1], buf[2], buf[3]);
 d.b = fourchar2int(buf[4], buf[5], buf[6], buf[7]);
 ...
 ... d.a ...
 ... d.b ...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List