in reply to HEX to floating point
You have an endianness issue. You are big endian. On my little endian Wintel machine:
# $h = unpack "H16", pack "d", 27.53152055; 27.53152055 => e566 13bb 1188 3b40 Now you have => 403b 8811 bb13 66e4 # $h = reverse unpack "h16", pack "d", 27.53152055; # 403b8811bb1366e5
I'll leave you to sort out the why's and wherefores and just give you some code to play with. You may well need to fiddle with it on your machine as "d" is native format double and your native format is big endian, unlike mine. By fiddle I mean try other templates 'h' and 'H' are similar but subtly different. 'N' and 'V' are big endian versus little endian 32 bit ints.
I have unpacked the ints into an "N" which is a long in "Network" (big endian) order which seems to be what you have given the results you desire. Also unless you have a 64 bit Perl and can therefore unpack using a quad (64 bit int) "q/Q" pack template you will need to use something like Math::BigInt if you need 64 bit ints. At the moment the hex2int sub just takes the least significant 32 bits and silently discards any higher order bits.
sub fix_endian { my $h = shift; my $h = reverse $h; $h =~ s/(.)(.)/$2$1/g; # only required if using 'H' not 'h' templ +ate return $h; } sub hex2int { unpack("N", pack("H8", substr('0'x8 .$_[0], -8))) } sub hex2float { unpack("f", pack("H8", substr('0'x8 .$_[0], -8))) } sub hex2double{ unpack("d", pack("H16", substr('0'x16 .$_[0], -16))) } while(<DATA>){ s/\s+//g; next unless $_; $, = "\t"; print $_, hex2int($_), hex2float($_), hex2double($_), hex2double(f +ix_endian($_)), "\n"; } __DATA__ 0000 0034 0000 31b1 01 91 403b 8811 bb13 66e4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: HEX to floating point
by Your Mother (Archbishop) on Jun 16, 2008 at 17:33 UTC | |
by tachyon-II (Chaplain) on Jun 16, 2008 at 23:31 UTC |