in reply to Network IEEE 754 to Native Floats: which pack/unpack ops better?
where perl takes care of translation between native long (big or little endian) and network long (always big endian), without you having to worry explicitly what is the underlying architecture.packing float as network long: +-----+ +-----------+ +------------+ |float|--->|native long|--->|network long| +-----+ +-----------+ +------------+ and unpacking network long back to float +------------+ +-----------+ +-----+ |network long|--->|native long|--->|float| +------------+ +-----------+ +-----+
Two separate tests were conducted -use strict; use IO::File; savefloat(); # <--- this is run from Sun E10000 readfloat(); # <--- this is run from Windows XP sub savefloat { my $original_float = 1234.5678; my $network_long = pack 'N', unpack 'L', pack 'f', $original_float; my $f = new IO::File "NetworkLong.txt", "w"; print $f $network_long, "\n"; } sub readfloat { my $f = new IO::File "NetworkLong.txt", "r"; chomp(my $network_long = <$f>); my $float = unpack 'f', pack 'L', unpack 'N', $network_long; printf "%0.4f\n", $float; }
Test 2On SUN: float->network_long->disk On WindowsXP: disk->network_long->float
I got the same result in both test 1 and test 2. This proves that the particular packing method worked cross-platform on both Sun and WindowsXP, which are big endian and little endian platforms respectively.On WindowsXP: float->network_long->disk On SUN: disk->network_long->float
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Network IEEE 754 to Native Floats: which pack/unpack ops better?
by shenme (Priest) on Nov 26, 2003 at 00:16 UTC |