in reply to casting to a 4 byte integer?
Do you mean an 8 byte integer to begin with? Since your intermediate layer is a Perl CGI, typecasting per se is not an option. But, you could either simply ensure that the value you wish to pass to the second C program doesn't exceed the capacity of an unsigned long (4 byte int), or use pack/unpack with the L option. Take a look at perlpacktut, specifically the "Integers" section.
Update: I believe in this case you'll need to pack the number into a binary format to begin with, and then unpack it before sending it off again. In your example unpack is assuming that the value supplied it is binary, and then doing some wierdness to give you the result you see. Instead, try this:
my $orig = 256000000; my $temp = pack('L', $orig); my $result = unpack('L', $temp);
Yields 256000000, which is what we want because that fits into a long. In contrast,
my $orig = 256000000000; my $temp = pack('L', $orig); my $result = unpack('L', $temp);
yields 4294967295 which is correct as 2**32 == 4294967296, the max storage of a long (according to Perl). Be sure to pay attention to the caveats in perlpacktut regarding the looseness of the C standard regarding the exact size of these types, making the exact conversion you want in this case dependant upon the compiler used in your C applications. HTH.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: casting to a 4 byte integer?
by cyberconte (Scribe) on Jan 04, 2003 at 03:46 UTC | |
|
Re: Re: casting to a 4 byte integer?
by cyberconte (Scribe) on Jan 04, 2003 at 05:20 UTC |