Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Converting Hex to Float (and vise versa) (overly simple)

by Anonymous Monk
on Feb 07, 2004 at 05:34 UTC ( [id://327297]=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 Re: Converting Hex to Float (and vise versa) (overly simple)
in thread Converting Hex to Float (and vise versa)


$float= unpack "f", pack "L", 0x428C0000;
print $float; # 70
$float= unpack "L", pack "f", 70 ;
print $float; # print 1116471296

Please tye, could you just point me why?
  • Comment on Re: Re: Converting Hex to Float (and vise versa) (overly simple)

Replies are listed 'Best First'.
Re: Re: Re: Converting Hex to Float (and vise versa) (overly simple)
by BrowserUk (Patriarch) on Feb 07, 2004 at 10:55 UTC

      Thanks, but the thing which hurt me is the 'no-symmetry':

      $float= unpack "f", pack "L", 0x428C0000; # $float is 70 $float= sprintf("%lx", unpack "L", pack "f", 70 ); print $float; # print 428C0000

      I take this as a fact but I don't understand why the exact
      opposite expression doesn't give back the original data:
      $is_not_hex = unpack "L", pack "f", 70 ;

        The reason has nothing to do with pack or unpack, it is the same reason that printing a hex constant doesn't print exactly what you see.

        print 0x428c0000; 1116471296

        Perl automatically converts hex values (as indicated by 0x...) to it internal (binary) representation when parsing the source code. It becomes just another perl number.

        Whenever perl prints out a number, it prints the ascii representation of it's decimal value, unless you specifically state that you want it printed in some other form. Ie. using printf or sprintf or similar.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://327297]
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.