From: "Philibert" <[EMAIL PROTECTED]>
> I have a problem with a program I did in Perl :
> I use the function system(@args) to launch an external command,
> and then get the exit value : $exit_value = $? >> 8.
> That runs fine, the problem is the exit value can be -1, so
> $exit_value should be a signed char. When the program returns -1, and
> I print $exit_value, I get 255...so I guess either it's an unsigned
> char, or an integer...is there a way to declare ma variable as a
> signed char? Thanks
No there is not.
But if you have a number in a Perl variable and want to treat it's
lowest byte as a signed int you could use something like this:
sub asSignedChar {
my $x = shift() + 0;
return ( $x & 0x80 ? -(~$x & 0x7F)-1 : $x);
}
$unsigned = 255;
$signed = asSignedChar($unsigned);
or
$signed = unpack('c',pack('C',$unsigned));
The pack solution will most probably be quicker.
HTH, Jenda
P.S.: Explanation of code:
my $x = shift() + 0;
= make sure we are working with a number,
convert a string containing some numbers to actual
number if necessary.
$x & 0x80
= If the highest bit of the lowest byte of the variable is set
~$x & 0x7F
= do binary negation of the number and forget everything
except lowest 7 bits
-(~$x & 0x7F)-1
= change the sign and subtract one
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
--- me
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]