Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109969
  • 博文数量: 39
  • 博客积分: 2530
  • 博客等级: 少校
  • 技术积分: 355
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-01 17:34
文章分类
文章存档

2011年(1)

2010年(28)

2009年(2)

2008年(8)

我的朋友

分类:

2008-07-21 14:05:36

Syntax

unpack TEMPLATE, STRING


Definition and Usage

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 % to indicate that you want a 16-bit checksum of the value of STRING, instead of the value.

Return Value

  • List of unpacked values

Here is the table which gives values to be used in TEMPLATE.

Character
Description
a
ASCII character string padded with null characters
A
ASCII character string padded with spaces
b
String of bits, lowest first
B
String of bits, highest first
c
A signed character (range usually -128 to 127)
C
An unsigned character (usually 8 bits)
d
A double-precision floating-point number
f
A single-precision floating-point number
h
Hexadecimal string, lowest digit first
H
Hexadecimal string, highest digit first
i
A signed integer
I
An unsigned integer
l
A signed long integer
L
An unsigned long integer
n
A short integer in network order
N
A long integer in network order
p
A pointer to a string
s
A signed short integer
S
An unsigned short integer
u
Convert to uuencode format
v
A short integer in VAX (little-endian) order
V
A long integer in VAX order
x
A null byte
X
Indicates "go back one byte"
@
Fill with nulls (ASCII 0)

Example

#!/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[0]\n";
print "Array $array[1]\n";
print "Array $array[2]\n";

It will produce following results:

bits are A
bits are
bits are ?rray 255
Array T
Array 30

阅读(1267) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~