develooper Front page | perl.beginners | Postings from September 2009

RE: decimal to binary?

Thread Previous | Thread Next
From:
Bob McConnell
Date:
September 23, 2009 13:26
Subject:
RE: decimal to binary?
Message ID:
FF8482A96323694490C194BABEAC24A004EB4B7D@Email.cbord.com
From: Bryan R Harris

>> From: Uri Guttman
>> 
>>>>>>>> "BM" == Bob McConnell <rvm@CBORD.com> writes:
>>> 
>>>   BM> From: Bryan R Harris
>>>>> 
>>>>> I need to convert a number like this:   -3205.0569059
>>>>> ... into an 8-byte double (big and little endian), e.g. 4f 3e 52
>> 00 2a
>>>   BM> bc 93
>>>>> d3  (I just made up those 8 byte values).
>>>>> 
>>>>> Is this easy in perl?  Are long and short ints easy as well?
>>> 
>>>   BM> The sprintf() family is your friend.
>>> 
>>> that will only generate text (hex and other formats). he needs pack
>>> which does exactly what he wants. read perlpacktut for a tutorial on
>>> pack/unpack and then perlfunc -f pack for the reference on it.
>> 
>> That statement just confuses me. His initial value of -3205.0569059
is
>> also text. It is the human readable representation of the number, and
is
>> not anything like what it looks like inside the computer. He just
asked
>> for a different format for that text. Why is sprintf not a reasonable
>> way to do that?
> 
> The 8 bytes is an IEEE 754-2008 formatted number -- see here for an
> explanation:
>  
>   http://en.wikipedia.org/wiki/Double_precision
> 
> It's not just a simple hex of a decimal...  You've got an exponent and
a
> sign encoded in there too.

OK, that I can understand. However, I don't see where that was expressed
or implied in the original query. Are you assuming that every current
architecture and Perl implementation uses that format to store double
precision numbers? Is that a safe assumption?

Bob McConnell

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About