Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Stumped by a pack/unpack problem

by esskar (Deacon)
on Feb 22, 2005 at 00:26 UTC ( [id://433208]=note: print w/replies, xml ) Need Help??


in reply to Stumped by a pack/unpack problem

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.

Replies are listed 'Best First'.
Re^2: Stumped by a pack/unpack problem
by crenz (Priest) on Feb 22, 2005 at 08:14 UTC

    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
Re^2: Stumped by a pack/unpack problem
by jmcnamara (Monsignor) on Feb 23, 2005 at 11:54 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://433208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-16 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found