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:56
Subject:
Re: decimal to binary?
Message ID:
87ws3psaez.fsf@quad.sysarch.com
>>>>> "BM" == Bob McConnell <rvm@CBORD.com> writes:

  BM> 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
  BM> 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.

  BM> That statement just confuses me. His initial value of -3205.0569059 is
  BM> also text. It is the human readable representation of the number, and is
  BM> not anything like what it looks like inside the computer. He just asked
  BM> for a different format for that text. Why is sprintf not a reasonable
  BM> way to do that?

he said number. perl will automatically convert a string number to a
float. but he wants access to the bytes in that float (hence his
mentioning of endian). perl can store a float, integer or string in a
scalar. but the float can only be used as a number, you can't get at its
bytes. pack solves that problem.

uri

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