feels wrong is probably because it is wrong. You have a network-order value that you need to convert to host-order value; I'll explain this backwards, as that's how perl has to process it.$float = unpack "f", pack "N", unpack "V", $data;
This treats the data as if it were in Little-endian order (which is it is not) and turns it into a 32-bit integer.unpack "V", $data;
This packs the data into a string in Big-endian (network) order. This is also wrong, because you now are using it on a little-endian machine.pack "N", $data;
This is one of those few cases where two wrongs *do* make a right; these operations are done in the wrong order, but the net effect is the same (which is why it works).
Your last suggestion,
is the most correct version. The unpack('N') unpacks the number as if it were a 32-bit value in network byte order (which it is), and the pack('L') re-packs the number as a 32-bit value in host byte order (which it is).pack 'L', unpack 'N', $data
In reply to Re: Network IEEE 754 to Native Floats: which pack/unpack ops better?
by Stevie-O
in thread Network IEEE 754 to Native Floats: which pack/unpack ops better?
by shenme
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |