Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Stumped by a pack/unpack problem

by crenz (Priest)
on Feb 22, 2005 at 00:05 UTC ( [id://433200]=perlquestion: print w/replies, xml ) Need Help??

crenz has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to fix a bug in Audio::OSC, where values are packed/unpacked incorrectly on some platforms and I've run into a problem I can't really solve:

According to the OSC Spec examples, a float value of 440.0 should be encoded as 43 dc 00 00. Using pack('f', 440.0'), I get these results:

  • Mac: 43 dc 00 00 - correct according to spec.
  • PC: 00 00 43 dc - incorrect.

A user suggested the somewhat unsightly combination pack("N", unpack("l", pack("f", 440.0))), but that yielded

  • Mac: 43 dc 00 00 - still correct
  • PC: dc 43 00 00 - still incorrect, but differently

Can somebody help me to come up with a good and fast solution for this? Even if it only works on PCs, it's okay -- I can check the endian-ness on startup and make aliases for the correct function to use. The main point is that it should be correct and fast.

Replies are listed 'Best First'.
Re: Stumped by a pack/unpack problem
by brian_d_foy (Abbot) on Feb 22, 2005 at 04:51 UTC

    One solution I've seen elsewhere turned the float into an int by multiplying by a set power of ten. The precision was set by the spec, so they knew they only need 4 decimal places. Then they packed the int in network order (that's the N in your second example). When they unpacked it, they divided by the same power of ten.

    If you want this to be really fast, you can cook up something simple in C (or even Inline::C) to do the same thing. Since you are using pack(), your Perl and C implementations will probably look the same so once you figure it out in Perl, it should be easy to translate to C.

    There was one time I had to do this for several gigs of data, and I actually pulled out the assembly language to do it. The target was the Motorola 68k family, which had several nice instructions to flip these things around (flip the words, then flip the bytes in the words). That was a huge win even over the C code. I only got away with that because I knew exactly which microprocessors we had to use for the problem (ah, the joys of custom built hardware!)

    Good luck!

    --
    brian d foy <bdfoy@cpan.org>
Re: Stumped by a pack/unpack problem
by esskar (Deacon) on Feb 22, 2005 at 00:26 UTC
    what about reverse pack("f", 440.0)

    Update1:
    The reason for the difference on MAC and PC is that the real numbers are in the native machine format only. For more information see perldoc -f pack and perldoc perlport.

    Update2:
    sorry, my first idea works just with unpack("H*", reverse pack("f", 440.0)) vs unpack("H*", pack("f", 440.0))

    By the way on my PC pack("f", 440.0) is 00 00 DC 43.
    and pack("N", unpack("l", pack("f", 440.0))) just returns 43 dc 00 00

    And the fastest solution would be a XS implementation.

      Hmm. That's an interesting result. Can you give me the output of

      print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x12345678))), "\n";

      on your system? XS won't be possible, by the way, as this is intentionally a pure-perl implementation.

        sure 0x78 0x56 0x34 0x12

      I'm not sure why you crossed out your first answer. It is correct; the way to change between big and little endian packed IEEE floats is to use reverse:
      $ perl -le 'print unpack "H*", reverse pack "f", 440' 43dc0000 $ perl -le 'print unpack "H*", pack "f", 440' 0000dc43

      --
      John.

Re: Stumped by a pack/unpack problem
by Aristotle (Chancellor) on Feb 23, 2005 at 11:26 UTC

    For completeness' sake: if you want some background on this madness, read about Endianness.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://433200]
Approved by kvale
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found