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

Re: decimal to binary?

Thread Previous | Thread Next
Uri Guttman
September 23, 2009 14:15
Re: decimal to binary?
Message ID:
>>>>> "BRH" == Bryan R Harris <> writes:

  BRH> Maybe this is just my own ignorance on big-endian vs. little endian, but
  BRH> this code:

  BRH>   print "big-endian:     ", unpack("H*", pack("d", -3205.0569059)), "\n";
  BRH>   print "little-endian:  ", unpack("h*", pack("d", -3205.0569059)), "\n";

  BRH> prints:

  BRH>   big-endian:     e626c5221d0aa9c0
  BRH>   little-endian:  6e625c22d1a09a0c

note that those are the same bytes but each byte is printed with
different ordering for the hex digits.

the docs say this:

                   h   A hex string (low nybble first).
                   H   A hex string (high nybble first).

and a nybble (or nibble) is a single hex digit of 4 bits and there are
two in a byte. the h vs H only changes the nibble order in a byte. this
has nothing to do with endian issues which are a byte ordering thing.

pack doesn't have a direct way to deal with endians with floats. you can
just use reverse on the byte string to control that. that is what i do
in Sort::Maker for this. did you look at that module yet? as i keep
saying it does exactly what you are trying to do so you can learn from it.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About