in reply to Float/double to hex?

Like the other monks, I’m not clear on the question being asked here. However, if floating point numbers in hexadecimal notation are wanted, Data::Float may be what you’re looking for:

#! perl use strict; use warnings; use Data::Float qw( float_hex hex_float ); my $n = 1 / 7; my $h = float_hex($n); my $d = hex_float($h); print "fraction = $n\nhex = $h\ndecimal = $d\n";

Output:

14:46 >perl 538_SoPW.pl fraction = 0.142857142857143 hex = +0x1.2492492492492p-3 decimal = 0.142857142857143 14:47 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Float/double to hex?
by Anonymous Monk on Feb 19, 2013 at 19:53 UTC
    I want to do some text analysis of stock data. I cannot do what I want using numbers, only alpha or hex. I want to convert the numbers to the four byte hex representation of a float (or eight bytes for a double). I didn't think to check CPAN. I'll try the above code. Thanks to all that replied. Mike
      I know a float is a four byte value. I want to have the hex string representation of those four bytes. If (2**16) is 65536, I want 65536 to be 0x0000ffff (or 0xffff0000, whatever order). Mike
        Here is what I am looking for (running inside "perl -d -e 1"): x 2**16 65536 x unpack('h*', pack('i', 65535)) 'ffff0000' $x = 1 / 7 x $x 0.142857142857143 x unpack('h*', pack('f', $x)) '529421e3'