Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Float/double to hex?

by edwyr (Sexton)
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

Replies are listed 'Best First'.
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.

        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.
        HA! I completely got fooled by wolfraudalpha
Re: Float/double to hex?
by Anonymous Monk on Feb 18, 2013 at 00:45 UTC
Re: Float/double to hex?
by Athanasius (Chancellor) 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?
[dazz]: Hi I have grabbed an image from an IP camera with Image::Grab. I then want to pass that image object to a subroutine where I do a read-only test on the image with Image:Magick.
[dazz]: How do I pass an image object to a subroutine and then apply a Image::Magick a read-only method to the image object???
[dazz]: Also, I want to use Image::Magick to annotate and composite (not in a sub). How do I take a Image::Grab object and apply Image::Magick write methods to it??
[dazz]: At present, I am saving the Image::Grab image to disk, then creating a new Image::Magick object that reads the disk file.
[dazz]: .

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-03-27 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (317 votes). Check out past polls.