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

Re: decimal to binary?

Thread Previous | Thread Next
From:
Bryan R Harris
Date:
September 23, 2009 17:37
Subject:
Re: decimal to binary?
Message ID:
C6E02965.36875%Bryan_R_Harris@raytheon.com

>>>>>> "BRH" == Bryan R Harris <Bryan_R_Harris@raytheon.com> 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.


Ah, this is the key I was missing!  Thanks.  Now I'm getting this:

value:  -3205.0569059
      big-endian:     c0 a9 0a 1d 22 c5 26 e6
      little-endian:  e6 26 c5 22 1d 0a a9 c0

I'm not sure whether I have my endians switched, but I'm putting my chances
of being right here at a little better than 50%.  =)  (Actually, I see your
$IS_BIG_ENDIAN check at the beginning of Sort::Maker, and borrowing that, I
think my chances are much better now that I got it right.)

As for looking at Sort::Maker, I have to admit I was a little scared at
first.  It's not obvious to me why you'd need to be packing doubles for a
sort routine, and I feared I'd get lost in the code...  That said, I found
the reverse part and it looked easy, so that's what I needed.

I think I'm good.  Thanks again to all.  Hope this makes good list-archives
fodder.

- Bryan






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