Function Name |
Pack |
Call syntax |
Formatstr = pack (packformat, list ); |
Explanation |
Converts a list or array to a simple variable in the format used by programming languages such as the actual storage format of the machine or C. The packformat parameter contains one or more formatting characters. Each element in the list corresponds to one. The format characters can be separated by spaces or tabs, because pack ignores spaces. In addition to the formats A, A, and @, you can add an integer after the format is repeated multiple times. For example: $ Twoints = pack ("I2", 103,241 ); Apply the same format to all elements with a * number, for example: $ Manyints = pack ("I *", 14, 26, 11, 83 ); For A and A, the following integer indicates the length of the string to be created. The repeat method is as follows: $ Strings = pack ("A6" X 2, "test1", "Test2 "); The format @ is special. An integer must be added after it. This number indicates the length required by the string. If the length is not enough, use null to supplement it. For example: $ Output = pack ("A @ 6 A", "test", "Test2 "); The most common purpose of the pack function is to create data that can interact with the C program. For example, strings in the C language end with null. You can create such data as follows: $ Cstring = pack ("ax", $ mystring ); The following table shows the equivalence relationship between some format characters and Data Types in C:
Character |
Equivalent c Data Type |
C |
Char |
D |
Double |
F |
Float |
I |
Int |
I |
Unsigned int (or unsigned) |
L |
Long |
L |
Unsigned long |
S |
Short |
S |
Unsigned short |
The complete format characters are shown in the following table. |
Format characters |
Description |
A |
String supplemented with null |
A |
String supplemented by Spaces |
B |
Bit String, before Low Position |
B |
Bit String, in front of the high position |
C |
Signed characters (usually-128 ~ 127) |
C |
Unsigned characters (usually 8 characters) |
D |
Double-precision floating point number |
F |
Single-precision floating point number |
H |
Hexadecimal number string, low front |
H |
Hexadecimal number string with a high position in front |
I |
Signed integer |
I |
Unsigned integer |
L |
Signed long integer |
L |
Unsigned long integer |
N |
Short Network order integer |
N |
Network sequence Length Integer |
P |
String pointer |
S |
Signed short integer |
S |
Unsigned short integer |
U |
Convert to uuencode format |
V |
VAX ordinal short integer |
V |
VAX ordinal long integer |
X |
An empty byte |
X |
Returns a byte. |
@ |
Fill with NULL bytes |
Function Name |
Unpack |
Call syntax |
@ List = unpack (packformat, formatstr ); |
Explanation |
In contrast to the pack function, unpack converts values stored in machine format to a list of Perl values. The format characters are basically the same as those of Pack (that is, the preceding table). The difference is that a format converts the machine format string to a Perl string and removes all spaces or empty characters at the end; X skips one byte. @ skips some bytes to the specified position. For example, @ 4 skips four bytes. Here is an example of @ and X contract: $ longrightint = unpack ("@ * X4 L", $ packstring ); This statement converts the last four bytes into unsigned long integers. Here is an example of decoding uuencode files:
1 :#! /Usr/local/bin/perl
2:
3: open (codedfile, "/u/janedoe/codefile") |
4: Die ("can't open input file ");
5: open (OUTFILE, "> OUTFILE") |
6: Die ("can't open output file ");
7: While ($ line = <codedfile> ){
8: $ decoded = unpack ("u", $ line );
9: Print OUTFILE ($ decoded );
10 :}
11: Close (OUTFILE );
12: Close (codedfile );
When using pack and unpack for uuencode, remember that although they are the same as the uuencode and uudecode tools in UNIX, they do not provide the first and last lines, if you want to use uudecode to decode the File Created by pack output, you must also output the first line and the last line (see uuencode help in UNIX ). |
|