Perl unpack Function
The Perl unpack function is the reversed of the pack function, taking a binary string as argument and expanding it out into a list of values. The string is unpacked using a format specified into a TEMPLATE. It returns the list of values resulting after unpacking the string.As I mentioned before, the reverse of this function is the pack function which concatenates into a string a list of values converted according to the formats specified by a template.
Generally, pack is used to write data in a specific format and unpack to get back the list of values that we passed to pack. Please be careful if you use pack on a machine and unpack on another machine with a different byte order, because unpack might interpret the data in a different way than pack wrote it.
So, in the following examples, to illustrate the behavior of the Perl
unpack function, we’ll use sometimes both
pack and
unpack.
The syntax formThe syntax form of the Perl unpack function is as follows:
LIST = unpack TEMPLATE, EXPR
The TEMPLATE consists of a sequence of characters as shown in the table below. One or more modifiers may follow some letters in the template (for instance, each letter may optionally be followed by a number giving a repeat count; or a * for the repeat count means to use however many items are left). The EXPR is a string expression representing a structure to be expanded and returned into a list.
The following table shows you some of the most frequent template characters for
pack and
unpack:
| a | A string with arbitrary binary data, will be null padded |
| A | A text (ASCII) string, will be space padded |
| b | A bit string (ascending bit order inside each byte, like vec()) |
| B | A bit string (descending bit order inside each byte) |
| c | A signed char (8-bit) value |
| C | An unsigned char (octet) value |
| d | A double-precision float in the native format |
| f | A single-precision float in the native format |
| h | A hex string (low nybble first) |
| H | A hex string (high nybble first) |
| i | A signed integer value |
| I | A unsigned integer value |
| l | A signed long (32-bit) value |
| L | An unsigned long value |
| n | An unsigned short (16-bit) in "network" (big-endian) order |
| N | An unsigned long (32-bit) in "network" (big-endian) order |
| s | A signed short (16-bit) value |
| S | An unsigned short value |
| U | A Unicode character number |
| v | An unsigned short (16-bit) in "VAX" (little-endian) order |
| V | An unsigned long (32-bit) in "VAX" (little-endian) order |
| x | A null byte |
| X | Back up a byte |
Next I’ll show you a few examples about the usage of some of these templates.
Padding a string with nulls or spaces
The following example shows you how to deal with the Perl unpack function and the 'a' template. Used with the pack function, this template let you pad a binary string with nulls. Used with the Perl unpack function, the 'a' template returns the full field as it is.
See the following code:
#!/usr/local/bin/perl
use strict;
use warnings;
my $str = pack 'a8', '32ab'; # "32ab\0\0\0\0"
foreach(unpack("(a1)*", $str)) {
print sprintf("%x", ord), " ";
}
print "\n";
# it prints: 33 32 61 62 0 0 0 0
$str = pack 'A8', '32ab'; # "32ab "
foreach(unpack("(a1)*", $str)) {
print sprintf("%x", ord), " ";
}
print "\n"
# it prints: 33 32 61 62 20 20 20 20
In the first example, the
'a' template was used to pad the string with nulls and in the second example the
'A' template was used to pack the string with spaces. In both cases we used the Perl
unpack function with the
'a' template. As you can notice, we get back the value of the string as it was packed by the
pack function.
A few words about the pack function from the first example. The $str is the string where the result will be returned, 'a8' is the template and '32ab' is the string to be converted. The 8 digit in the template is a modifier and it means that it will be appended so many null bytes until the resulting string will have 8 characters length. In the second example pack was used with the template 'A8' and it will result a string having 8 characters, the last four characters being spaces.
The template for the Perl unpack function uses parentheses to group things (like the regular expressions do) and the group is followed by a * repeat count which means to use how many items are left.
The sprintf function allows you to convert in hexadecimal the elements of the list returned by the Perl unpack function. This function was used to see the hexadecimal values of the characters.
The 'A' template is similar with the previous one, except that space is used instead of null. Used with the pack function, this template let you pad an ASCII string with spaces. If you use it with the Perl unpack function, the 'A' template replaces trailing spaces with nulls.
To illustrate this, I’ll rewrite the previous example as:
#!/usr/local/bin/perl
use strict;
use warnings;
# the string is padded with spaces
my $str = pack 'A8', '32ab'; # "32ab "
foreach(unpack("(A1)*", $str)) {
print sprintf("%x", ord), " ";
}
print "\n";
# it prints: 33 32 61 62 0 0 0 0
# the string is padded with nulls
$str = pack 'a8', '32ab'; # "32ab\0\0\0\0"
foreach(unpack("(A1)*", $str)) {
print sprintf("%x", ord), " ";
}
print "\n"
# it prints: 33 32 61 62 0 0 0 0
As you can see, if you use the Perl
unpack function with the
'A' template, the trailing spaces will be replaced by nulls. The trailing nulls will remain unchanged.
Packing and unpacking bit stringsThe 'b' and 'B' formats of the pack function packs strings consisting of 0 and 1 characters to bytes. The Perl unpack function get back the list of 0’s and 1’s from the bit string.
A byte consists of a group of 8 bits as in the following figure:
LSB means here the least significant bit and it is sometimes referred as the rightmost bit. MSB is the most significant bit and is sometimes referred as the leftmost bit. In the above example, MSB = 1 and LSB = 0.
For an example, let’s say you want to represent the decimal number 178 into a string of bits. You can write this number either as '10110010' starting with MSB or as '01001101' starting with LSB. For these representations, Perl has respectively two templates: 'B' and 'b'.
The following example shows you how to pack the above number using the two templates:
# starting with MSB
my $nr = ord pack ('B8', '10110010');
print "$nr\n";
# it prints 178 (128 + 32 + 16 + 2)
# starting with LSB
$nr = ord pack ('b8', '01001101');
print "$nr\n";
# it prints 178 (2 + 16 + 32 + 128)
In this representation, the count refers to the number of bits to be packed - in the above example the count is
8.
You can use the pack function with the 'b*' format to translate a string of 0’s and 1’s into a bit string, and the Perl unpack function to get back the list of 0’s and 1’s from the bit string (the '*' is like a wildcard for more of the same). It is important to use the same format ('b' or 'B' ) for both pack and unpack functions (i.e. if you packed a number with the 'b' template, you must use the same template to unpack the bits string).
Here’s an example about how to use the Perl unpack function with the 'b' template:
#!/usr/local/bin/perl
use strict;
use warnings;
my @bitArray = qw(1 0 0 0 1 1 1 1 0 0 1 1);
my $bitString = pack 'b*', join('', @bitArray);
@bitArray = split(//, unpack('b*', $bitString));
print "@bitArray\n";
# it prints: 1 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0
Please note that our initial array of bits had
12 elements only, so the
pack function initialized the last
4 bits of the
$bitString with
0.
You can rewrite the previous example by using both pack and unpack functions with the 'B*' format:
#!/usr/local/bin/perl
use strict;
use warnings;
my @bitArray = qw(1 0 0 0 1 1 1 1 0 0 1 1);
my $bitString = pack 'B*', join('', @bitArray);
@bitArray = split(//, unpack('B*', $bitString));
print "@bitArray\n";
# it prints: 1 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0
Table of Contents:
A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
Array Size
Array Length
Perl Hashes
Perl Statements
Perl if
Perl unless
Perl switch
Perl while
Perl do-while
Perl until
Perl do-until
Perl for
Perl foreach
Built-in Perl Functions
Functions by Category
String Functions
Regular Expressions and Pattern Matching
List Functions
Array Functions
Hash Functions
Miscellaneous Functions
Functions in alphabetical order
chomp
chop
chr
crypt
defined
delete
each
exists
grep
hex
index
join
keys
lc
lcfirst
length
map
oct
ord
pack
pop
push
q
qq
qw
reverse
rindex
scalar
shift
sort
splice
split
sprintf
substr
tr
uc
ucfirst
undef
unpack (more)
unshift
values
return from Perl unpack Function to Perl Basics
Would you like to create your own website like this one?
Hit the Alarm Clock!