D
Dov Levenglick
Hi,
i am attaching a snippet of code and two results that it produces. the
first result is when the printf line is not commented out while the
second result is when the line is commented. the code was run on a
UNIX station running SunOS 5.8
result #1:
big_endian: 123456 78
big_endian: 1234 7856
big_endian: 12 785634
big_endian: 0 78563412
Result: 12345678 78563412
result #2:
Result: 12345678 ffffffff
i am aware that $a reaches the value of 0 only after many divisions by
256 (since divisions are in floating point context) and therefore
$result reaches Infinity (hence ffffffff), my question is how and why
does the printf change the context of $a from floating point to
integer.
btw, i am aware that "use integer" or $a=int($a/256)" would avoid the
problem as well, therefore please dont offer them as an answer to my
question.
my $a = 0x12345678;
$b = &big_endian($a);
printf ("Result: %x %x\n",$a,$b);
sub big_endian {
my $data = shift;
my $result = 0;
while ($data) {
$result *= 256;
$result += $data%256;
$data /= 256;
printf "big_endian: %x %x\n",$data,$result;
} return ($result);
}
i am attaching a snippet of code and two results that it produces. the
first result is when the printf line is not commented out while the
second result is when the line is commented. the code was run on a
UNIX station running SunOS 5.8
result #1:
big_endian: 123456 78
big_endian: 1234 7856
big_endian: 12 785634
big_endian: 0 78563412
Result: 12345678 78563412
result #2:
Result: 12345678 ffffffff
i am aware that $a reaches the value of 0 only after many divisions by
256 (since divisions are in floating point context) and therefore
$result reaches Infinity (hence ffffffff), my question is how and why
does the printf change the context of $a from floating point to
integer.
btw, i am aware that "use integer" or $a=int($a/256)" would avoid the
problem as well, therefore please dont offer them as an answer to my
question.
my $a = 0x12345678;
$b = &big_endian($a);
printf ("Result: %x %x\n",$a,$b);
sub big_endian {
my $data = shift;
my $result = 0;
while ($data) {
$result *= 256;
$result += $data%256;
$data /= 256;
printf "big_endian: %x %x\n",$data,$result;
} return ($result);
}