in reply to unpack offset question

( Please avoid putting your entire post in code tags. Just put <p> at the start of every paragraph instead. )

You didn't specify byte order and whether the numbers are signed or unsigned.

@fields = unpack('n3', $_); # unsigned big-endian @fields = unpack('v3', $_); # unsigned little-endian @fields = unpack('S3', $_); # unsigned native @fields = unpack('n!3', $_); # signed big-endian @fields = unpack('v!3', $_); # signed little-endian @fields = unpack('s3', $_); # signed native

You provided even less information about the strings.

Update: n! and s! were introduced in 5.10. Pre 5.10,

# signed big-endian @fields = map { unpack 's', pack 'S', $_ } unpack('n3', $_); # signed little-endian @fields = map { unpack 's', pack 'S', $_ } unpack('v3', $_);

Update: Oops, had S for both signed and unsigned at the top. Thanks AnomalousMonk.

Replies are listed 'Best First'.
Re^2: unpack offset question
by magawake (Novice) on Mar 28, 2009 at 12:09 UTC
    Thankyou for the kind response.
  • All field sizes are fixed and constant.
  • All fields are contiguous. There is no explicit 'padding' between fields regardless of the data types, size and allignment issues.
  • The binary fields are provided in Big Engined Format which is unsigned
  • ASCII strings are left align, null padded
      'Z6' for a NUL padded str 6-bytes long. The NULs, if any, are removed.