in reply to Word reverse
unpack knows how to deal efficiently with little/big-endian unsigned shorts without you having to do the byte-swapping yourself.
In recent perl's there are actually two mechanisms:
It's also worth noting that it is far more efficient to use a quantity specifier (number or * after the template character), than to step through the string picking out the individual words yourself.
This demonstrates both template options and both quantity specifiers options. (either could be used with either):
#! perl -slw use strict; my $raw = "\x45\xd2\xff\x76\x89\x3a\x00\x33"; my @leUShorts = unpack 'v4', $raw; print "@leUShorts"; my @leUShorts2 = unpack 'S<*', $raw; print "@leUShorts2"; my @beUShorts = unpack 'n4', $raw; print "@beUShorts"; my @beUShorts2 = unpack 'S>*', $raw; print "@beUShorts2"; __END__ C:\test>junk67 53829 30463 14985 13056 53829 30463 14985 13056 17874 65398 35130 51 17874 65398 35130 51
|
|---|