3

It's my first SO question, so please be gentle. :)

I'm not a programming expert. I've got a program I'm playing with. It came with some BMP files. I still have the BMP files, but I'm converting them to C code instead. Currently, I'm loading them thusly:

static char bits[] = {
0xff,0xff,0xff,0xff,0xff,0xab,0xaa,0x5a,0x55,0xd5,0x55,0x55,0xb5,0xaa,0xaa,
0xab,0xaa,0x5a,0x55,0xd5,0x55,0x55,0xb5,0xaa,0xaa,0xab,0xaa,0x5a,0x55,0xd5,
[blah blah blah]
0xff,0xff,0xff,0xff,0xff};
walls.pixmap = XCreateBitmapFromData(dpy,DefaultRootWindow(dpy),bits,40,40);

Each of the characters that is greater than 0x80 is generating this warning:

bitmaps.c:38: warning: overflow in implicit constant conversion

So I tried changing my definition to

static unsigned char bits[] = {

but that displays a new warning:

bitmaps.c:31: warning: pointer targets in passing argument 3 of 'XCreateBitmapFromData' differ in signedness
/usr/include/X11/Xlib.h:1607: note: expected 'const char *' but argument is of type 'unsigned char *'

Is there a way to load bitmaps that will compile without warnings? Should I just accept that warnings will always appear? Should I be doing something different since I have the raw BMP files anyway?

Thanks.

4

1 に答える 1

1

Using unsigned chars should prevent the warning.

Signed chars can not actually represent the literals (which are of type int, not char).

static unsigned char bits[] = {
    0xff,0xff,0xff,0xff,0xff,0xab,0xaa,0x5a,0x55,0xd5,0x55,0x55,0xb5,0xaa,0xaa,
    0xab,0xaa,0x5a,0x55,0xd5,0x55,0x55,0xb5,0xaa,0xaa,0xab,0xaa,0x5a,0x55,0xd5,
    /* [blah blah blah] */
    0xff,0xff,0xff,0xff,0xff};

walls.pixmap = XCreateBitmapFromData(dpy,DefaultRootWindow(dpy),(char*) bits,40,40);

In other words, you could also explicitely cast each literal:

static char bits[] = {
    (char)0xff,(char)0xff,(char)0xff,(char)0xff,(char)0xff,(char)0xab,(char)0xaa,(char)0x5a,(char)0x55,(char)0xd5,(char)0x55,(char)0x55,(char)0xb5,(char)0xaa,(char)0xaa,
    (char)0xab,(char)0xaa,(char)0x5a,(char)0x55,(char)0xd5,(char)0x55,(char)0x55,(char)0xb5,(char)0xaa,(char)0xaa,(char)0xab,(char)0xaa,(char)0x5a,(char)0x55,(char)0xd5,
    /* [blah blah blah] */
    (char)0xff,(char)0xff,(char)0xff,(char)0xff,(char)0xff};

but that seems a little bit more awkward to me

于 2012-10-08T17:09:23.510 に答える