當前位置:首頁 » Perl » perl unpack()函數

perl unpack()函數

perl unpack()函數例子,unpack()函數實例代碼 - 解壓縮的二進製字符串STRING 使用模板TEMPLATE 中指定的格式字符串。

語法

unpack TEMPLATE, STRING


定義和用法

解壓縮的二進製字符串STRING 使用模板TEMPLATE 中指定的格式字符串。基本上包反轉操作,返回到所提供的格式,所述填充值的列表。

您也可以任何格式的字段的前綴%<數字>表示你想要一個16位的校驗和值的字符串,而不是值。

返回值

  • 解壓後的值列表

下麵是表,它給出在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)

例子

#!/usr/bin/perl -w
#by www.gitbook.net

$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";

這將產生以下結果:

bits are A
bits are
bits are �T
Array 255
Array T
Array 30