Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Don't ask to ask, just ask
 
PerlMonks  

Re: Converting Hex to Float (and vise versa)

by jmcnamara (Monsignor)
on Feb 06, 2004 at 16:51 UTC ( [id://327230]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Converting Hex to Float (and vise versa)


Here is one way of doing it:
#!/usr/bin/perl -wl my $hex = '0x428C0000'; $hex =~ s/0x//i; # Strip the hex 0x if present print unpack "f", reverse pack "H*", $hex ; print unpack "f", pack "H*", $hex ; __END__ Prints on an a little endian platform: 70 5.03150226600469e-041

In this case the first of these looks like the right value.

It is rare that you will encounter a platform where one of these doesn't work. If you do then I have some Perl code for packing and unpacking IEEE 754 floats and doubles independently of the architecture.

--
John.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://327230]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.