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

    I realize those subs contain a trivial amount of code but a packtard like me sure wouldn't mind seeing those gathered together in a module with a nice side of Pod. (Think of the fame, wealth, and women that could be yours!)

      Sadly they are not portable due to the endianness of the source data and then the endianness of the destination reader system. You can find 3 diffferent ways to unpack hex strings in faq4 from memory (using hex, pack/unpack, and Bit::Vector])

      As you may have noted BrowserUk presented a method that converts the data given from the OPs system (big endian) to his system (little endian wintel). When the OP tried to run it he found it broken.

      Pack/Unpack Tutorial (aka How the System Stores Data) by pfaut is an excellent resource for understanding pack/unpack.