Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Float/double to hex?

by edwyr (Acolyte)
on Feb 18, 2013 at 00:30 UTC ( #1019249=perlquestion: print w/ replies, xml ) Need Help??
edwyr has asked for the wisdom of the Perl Monks concerning the following question:

I know floats and doubles are IEEE. How do I convert the float (four bytes) to hex. I need the next for a project I'm working on where numbers are not convenient. (i.e. 1 / 7 = 0.142857142857143 = 0x6FDF1234 ($whatever)) Mike

Comment on Float/double to hex?
Re: Float/double to hex?
by Anonymous Monk on Feb 18, 2013 at 00:45 UTC
Re: Float/double to hex?
by BrowserUk (Pope) on Feb 18, 2013 at 01:27 UTC
    1 / 7 = 0.142857142857143 = 0x6FDF1234

    That hex value doesn't correspond to either a float or double in IEEE format?

    print unpack 'h*', pack 'f', 1/7;; 529421e3 print unpack 'h*', pack 'd', 1/7;; 2942942942942cf3

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
        HA! I completely got fooled by wolfraudalpha

        I've no idea what that hex value is, but it certainly isn't any IEEE representation of 1/7.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Float/double to hex?
by Athanasius (Prior) on Feb 19, 2013 at 04:53 UTC

    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,

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019249]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-07-14 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (268 votes), past polls