Discussion:
Strange behavior
(too old to reply)
Reinhard Pagitsch
2005-10-14 07:03:24 UTC
Permalink
Hello,

I have an other problem which I do not understand:

Here the code piece:

#define NOP 0xd3eeee
void WriteNOP(PerlIO* fh)
{
WriteTriplex(fh, NOP);
}


int WriteTriplex(PerlIO* fh, long triplex)
{
int i;
char buf[4] = { '\0' };
printf("The triplex: %x\n", triplex);
buf[0] = (char)triplex>>16;
buf[1] = (char)triplex>>8;
buf[2] = (char)triplex;
printf("The triplex: %s\n", buf);
return PerlIO_write(fh, buf, 3);
}


If I do this in the file I get FFFFEE and not as expected D3EEEE.
The printf statements but showing me the the correct output.

Does anyone know why?

regards,
Reinhard
Muppet
2005-10-14 14:13:12 UTC
Permalink
Post by Reinhard Pagitsch
Hello,
#define NOP 0xd3eeee
void WriteNOP(PerlIO* fh)
{
WriteTriplex(fh, NOP);
}
int WriteTriplex(PerlIO* fh, long triplex)
{
int i;
char buf[4] = { '\0' };
printf("The triplex: %x\n", triplex);
buf[0] = (char)triplex>>16;
buf[1] = (char)triplex>>8;
buf[2] = (char)triplex;
printf("The triplex: %s\n", buf);
return PerlIO_write(fh, buf, 3);
}
That's a hard way to do it. You're still suffering from endianness problems
because you're assuming which byte is first, *and* you're suffering from
signed shifts.

Try this, instead:

#define NOP "\xd3\xee\xee" /* or whatever is proper C for hex char vals */
#define WRITE_CONST_STRING(perl_io, const_buffer) \
PerlIO_Write ((perl_io), (const_buffer), sizeof (const_buffer))

The benefit here is that the compiler can figure out for itself the length
parameter because the string is a compile-time constant. Since you're using
strings (uchar arrays), you have no worries about byte order. Since you're
using bytes, you have no worries about bit-shifting.
--
muppet <scott at asofyet dot org>
Loading...