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

Re: decimal to binary?

Thread Previous | Thread Next
From:
Uri Guttman
Date:
September 23, 2009 12:13
Subject:
Re: decimal to binary?
Message ID:
874oqttqys.fsf@quad.sysarch.com
>>>>> "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.

pack can take a perl float and make a string of the internal
representation in either endian order.

for some real world examples of doing that, check out the code in
Sort::Maker which packs floats into strings and deals with endian issues
too.

uri

-- 
.signature_stem

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