in reply to Re: Problems Getting the Template Correct when Using unpack()
in thread Problems Getting the Template Correct when Using unpack()

By way of some more clarification, BTW...

From reading pack, there was something about how 'A' strips whitespace, etc and 'a' doesn't do that when using unpack. For no particular reason, I wanted to retain the entire field, hence the usage of 'a' in the template.

  • Comment on Re^2: Problems Getting the Template Correct when Using unpack()

Replies are listed 'Best First'.
Re^3: Problems Getting the Template Correct when Using unpack()
by haukex (Archbishop) on Oct 11, 2017 at 07:58 UTC
    'A' strips whitespace, etc and 'a' doesn't do that

    True, but I think BillKSmith's point was that you were using 'C' instead of 'a'. From pack:

    a A string with arbitrary binary data, will be null padded. A A text (ASCII) string, will be space padded. Z A null-terminated (ASCIZ) string, will be null padded. C An unsigned char (octet) value.

    Here's the difference:

    use Data::Dump; my $data = "\x01\x02\x03\x20\x00"; dd unpack 'C*', $data; # prints (1, 2, 3, 32, 0) dd unpack 'a*', $data; # prints "\1\2\3 \0" dd unpack 'A*', $data; # prints "\1\2\3" dd unpack 'Z*', $data; # prints "\1\2\3 " dd unpack 'Z*', $data."X "; # prints "\1\2\3 " dd unpack 'A*', $data."X "; # prints "\1\2\3 \0X" dd unpack 'a*', $data."X "; # prints "\1\2\3 \0X "

    So if you want to retain all of the binary data as a string for further unpacking, the 'a' template is the way to go. 'C' will split that string into its individual bytes and return those values.

    Update: Added the second 'A*' example.