Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Converting binary to double precision

by ahoriuch (Acolyte)
on Feb 24, 2014 at 18:21 UTC ( #1076017=perlquestion: print w/replies, xml ) Need Help??

ahoriuch has asked for the wisdom of the Perl Monks concerning the following question:

I'm having difficulties converting binary to double precision floating point. Using an on-line calculator, here is what the number should be: 5.71713084433028663131071134273E-5 Here's the number in hex:

unpack("H16",$buffer); #Returns 3f0d f967 3344 c570

Here is the number in "d":

unpack("d",$buffer); #Returns 1.69044404965372e+235

Something is clearly wrong. Reading the unpack documentation, there is no option for "D". Is there another way I can convert this number?

Replies are listed 'Best First'.
Re: Converting binary to double precision (big-endian)
by BrowserUk (Pope) on Feb 24, 2014 at 19:06 UTC

    Your value comes from a big-endian system, so you'll need 'd>' to decode it:

    $n = pack 'H16', '3f0df9673344c570';; print unpack 'd>', $n;; 5.71713084433029e-005

    You could also do the byte swapping yourself:

    $n = pack 'H16', '3f0df9673344c570';; print unpack 'd', scalar reverse $n;; 5.71713084433029e-005

    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.

      In my case, this was the correct answer. I needed to switch from big to little-endian. It's amazing how little there is on the internet on this subject. Hopefully this will help future users.

      my $n = pack 'H16', '3f0df9673344c570';; printf "Big\n"; print unpack ("d", scalar reverse $n); #Big #5.71713084433029e-05 printf "\nLittle\n"; print unpack ("d",$n); printf "\n"; #Little #1.69044404965372e+235
Re: Converting binary to double precision
by toolic (Bishop) on Feb 24, 2014 at 18:25 UTC
    Reading the unpack documentation, there is no option for "D"
    pack
    D A float of long-double precision in native format. (Long doubles are available only if your system supports long double values _and_ if Perl has been compiled to support those. Raises an exception otherwise.)

      Is there a specific version/ install that I need? Invalid type 'D' in unpack at ... line 112, <FILE> chunk 1.

        I'm running on 5.12.2, and I see it. What version of perl do you use?

        UPDATE: I don't see it in 5.8.8. Also, I can't find any mention of a change to pack in either perl5100delta or perl5120delta.

Re: Converting binary to double precision
by Anonymous Monk on Feb 24, 2014 at 18:42 UTC
    If you know that you are dealing with an x86 platform, this approach might work, but Perl is platform-independent. The binary representation of a floating point number among CPU-types is not the same. For that matter, there is more than one floating-point type available on a single CPU. The binary representation will vary considerably.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-10-20 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (209 votes). Check out past polls.

    Notices?