This function unpacks the binary string STRING using the format specified in TEMPLATE. Basically reverses the operation of pack, returning the list of packed values according to the supplied format.
You can also prefix any format field with a %<number> to indicate that you want a 16-bit checksum of the value of STRING, instead of the value.
Following is the simple syntax for this function −
unpack TEMPLATE, STRING
This function returns the list of unpacked values.
Here is the table which gives values to be used in TEMPLATE.
|Sr.No.||Character & Description|
ASCII character string padded with null characters
ASCII character string padded with spaces
String of bits, lowest first
String of bits, highest first
A signed character (range usually -128 to 127)
An unsigned character (usually 8 bits)
A double-precision floating-point number
A single-precision floating-point number
Hexadecimal string, lowest digit first
Hexadecimal string, highest digit first
A signed integer
An unsigned integer
A signed long integer
An unsigned long integer
A short integer in network order
A long integer in network order
A pointer to a string
A signed short integer
An unsigned short integer
Convert to uuencode format
A short integer in VAX (little-endian) order
A long integer in VAX order
A null byte
Indicates "go back one byte"
Fill with nulls (ASCII 0)
Following is the example code showing its basic usage −
#!/usr/bin/perl -w $bits = pack("c", 65); # prints A, which is ASCII 65. print "bits are $bits\n"; $bits = pack( "x" ); # $bits is now a null chracter. print "bits are $bits\n"; $bits = pack( "sai", 255, "T", 30 ); # creates a seven charcter string on most computers' print "bits are $bits\n"; @array = unpack( "sai", "$bits" ); #Array now contains three elements: 255, A and 47. print "Array $array\n"; print "Array $array\n"; print "Array $array\n";
When above code is executed, it produces the following result −
bits are A bits are bits are T- Array 255 Array T Array 30