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 2008-06-10 14:11

Name: Anonymous 2008-06-10 14:32

this is the opposite direction of what i want to do

Name: Anonymous 2008-06-10 14:40

unpack it

Name: Anonymous 2008-06-10 14:43

Use PIL instead of reading the goddamn header yourself, idiot.

Name: Anonymous 2008-06-10 14:54

The struct module will help (http://docs.python.org/lib/module-struct.html)

So your program would become:


from struct import unpack

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

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


Name: Anonymous 2008-06-10 14:57

>>5
This is the correct answer.

>>6
Never use struct without byte order annotations.

Name: Anonymous 2008-06-10 23:50

($w,$h)=map{unpack "N"} $w,$h;
list context

Name: Anonymous 2008-06-11 4:41

THE FORCED CONVERSION OF INTEGERS

Name: Anonymous 2008-06-11 5:09

(>>[i]Post truncated.[/i])

Name: Anonymous 2008-06-11 7:42

>>9
Convert to Integer or die.

Name: Anonymous 2008-06-11 7:43

>>10
BBCODE TRUNCATED

Name: Anonymous 2008-06-11 8:43

Yeah, load the whole fucking file into memory to read two fields.

Name: Anonymous 2008-06-11 8:55

>>13
memory is cheap. lines of forcibly indented code are expensive.

Name: Anonymous 2008-06-11 9:11

>>13
He's going to work with all this data so it's ok.

Name: Anonymous 2008-06-11 10:46

i am a heron. i ahev a long neck and i pick fish out of the water w/ my beak. if you dont repost this comment on 10 other pages i will fly into your kitchen tonight and make a mess of your pots and pans

Name: Sgt.Kabukiman눦죖 2012-05-23 15:26

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: Anonymous 2012-05-25 2:56

What kind of a moron uses the coding style:
function( arg):

Go fuck yourself

Name: Anonymous 2012-05-25 3:02

>>18
fuck off fagstorm

Name: Anonymous 2012-05-25 19:56

I( am( a( faggot())))

Name: Anonymous 2012-05-26 0:26

Why the fuck do you need to use a module to do this sort of thing? What the fuck?

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-05-26 7:11

This is the sort of thing that happens when you try to use a scripting language oriented towards text processing for binary data.

struct {
 int a;
 int b;
} d;
...
 fread(&d, 1, sizeof(d), pfile);
 ...
 ... d.a ...
 ... d.b ...


Or if you don't care for defining structures/the offsets aren't fixed just use *(your_desired_type)(ptr + byte_offs) to pick out the pieces directly.

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 ...

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-05-26 8:07

>>23
Inefficient.

If by "not portable" you mean the size of the types, just use the appropriate size (C99 has some defined, I never bother to use them because I know what size my ints/shorts/longs are for the platform I'm developing on.)

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-05-26 8:08

But this is a Python thread so even that solution is going to be at least an order of magnitude more efficient...

Name: Anonymous 2012-05-26 8:23

>>23
this has to be the shittiest code I've seen in /prog/
and he goes all this way, without even taking care of endianity

Name: Anonymous 2012-05-26 12:22

>>25
The struct module is written in C, I don't understand your complaint.

Name: Anonymous 2012-05-26 13:46

>>27
Protip: neither does he.

Name: Anonymous 2012-05-26 15:07

CONVERT TO ISLAM

Name: OP 2012-05-26 15:11

4 years after thread started and still no solution
come on guys!!!
I was 12 when i started this thread and now I'm 24

Name: Anonymous 2012-05-26 18:34

If you've really gone 4 years without thinking to google it, you're beyond hope.

Name: Anonymous 2012-05-26 19:59

>>30
yea

Name: RICHARD STALLMAN 2012-05-26 19:59

>>30
yea

Name: THE SUSSMAN 2012-05-26 20:00

>>30
yea

Name: GEORGE COSTANZA 2012-05-26 20:00

>>30
yea

Name: Anonymous 2012-05-27 6:00

lumda cuck ya ass

Name: « 23 » 2012-05-27 12:03

>>24,26
It doesn't have to care about endianness as it performs the same operation on both little-endian and big-endian machines. int should be changed to long for maximum portability though.
For efficiency, you can replace the separate arguments with a pointer and use conditional compilation:
static long fourchar2int(unsigned char* b)
{
#ifdef X86
        return *((int*) b);
#else
        if (b[3] & 0x80) {
                return (~b[3] & 0xff) * -16777216
                     + (~b[2] & 0xff) * -65536
                     + (~b[1] & 0xff) * -256
                     + (~b[0] & 0xff) * -1
                     - 1;
        } else {
                return b[3] * 16777216
                     + b[2] * 65536
                     + b[1] * 256
                     + b[0] * 1;
        }
#endif
}

Name: Anonymous 2012-05-27 18:46

typedef long dong;
static dong up_butt(unsigned char* b)
{
#ifdef CIRCUMCISED /* for Israeli CPUs */
        return *((dong*)b);
#else /* for the Goyim */
        dong anal = b[3] * 16777216
                     | b[2] * 65536
                     | b[1] * 256
                     | b[0] * 1;
        return b[3] & 128 ? -anal : anal;
#endif
}

ZOMG OPTIMIZED!

Name: bampu pantsu 2012-05-29 3:47

bampu pantsu

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