pack

(PHP 4, PHP 5)

pack — Pack data into binary string

Report a bug

说明

string pack ( string $format [, mixed $args [, mixed $... ]] )

Pack given arguments into binary string according to format.

The idea for this function was taken from Perl and all formatting codes work the same as in Perl. However, there are some formatting codes that are missing such as Perl's "u" format code.

Note that the distinction between signed and unsigned values only affects the function unpack(), where as function pack() gives the same result for signed and unsigned format codes.

Report a bug

参数

format

The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string.

Currently implemented formats are:

pack() format characters
Code Description
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
c signed char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte
X Back up one byte
@ NUL-fill to absolute position
args
Report a bug

返回值

Returns a binary string containing data.

Report a bug

范例

Example #1 pack() example

<?php
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);
?>

The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.

Report a bug

注释

Caution

Note that PHP internally stores integer values as signed values of a machine-dependent size (C type long). Integer literals and operations that yield numbers outside the bounds of the integer type will be stored as float. When packing these floats as integers, they are first cast into the integer type. This may or may not result in the desired byte pattern.

The most relevant case is when packing unsigned numbers that would be representable with the integer type if it were unsigned. In systems where the integer type has a 32-bit size, the cast usually results in the same byte pattern as if the integer were unsigned (although this relies on implementation-defined unsigned to signed conversions, as per the C standard). In systems where the integer type has 64-bit size, the float most likely does not have a mantissa large enough to hold the value without loss of precision. If those systems also have a native 64-bit C int type (most UNIX-like systems don't), the only way to use the I pack format in the upper range is to create integer negative values with the same byte representation as the desired unsigned value.

Report a bug

参见

  • unpack() - Unpack data from binary string
php_check_syntax ignore_user_abort


[edit] Last updated: Fri, 20 Apr 2012
add a note User Contributed Notes pack
chadm at codeangel dot org 23-Feb-2012 09:13

If you'd like to understand pack/unpack. There is a tutorial here in perl, that works equally well in understanding it for php:

http://perldoc.perl.org/perlpacktut.html

Gerjoo at gmail dot com 26-May-2011 12:55

You can use pack to strip the byte order mark (BOM) from a file.

For example, strip the UTF-8 BOM:

<?php

// Strips the UTF-8 mark: (hex value: EF BB BF)
function trimUTF8BOM($data){
    if(substr($data, 0, 3) == pack('CCC', 239, 187, 191)) {
        return substr($data, 3);
    }
    return $data;
}

?>

This function could be easily adjusted to match any byte order mark. Have a look at wikipedia for a full list of hex codes for each specific encoding.

- Gerard

FrozenFire 20-Oct-2010 09:40

If you need to unpack a signed short from big-endian or little-endian specifically, instead of machine-byte-order, you need only unpack it as the unsigned form, and then if the result is >= 2^15, subtract 2^16 from it.

And example would be:

<?php
$foo = unpack("n", $signedbigendianshort);
$foo = $foo[1];
if($foo >= pow(2, 15)) $foo -= pow(2, 16);
?>

Ammar Hameed 21-Jun-2010 04:53

Using pack to write Arabic char(s) to a file.

<?php
$text = "㔆㘆㘆";

$text = mb_convert_encoding($text, "UCS-2BE", "HTML-ENTITIES");

$len =  mb_strlen($text);

$bom = mb_convert_encoding("&#65534;", "unicode", "HTML-ENTITIES");

$fp = fopen('text.txt', 'w');

fwrite($fp, pack('a2', $bom)); 
fwrite($fp, pack("a{$len}", $text));
fwrite($fp, pack('a2', $bom));
fwrite($fp, pack('a2', "\n"));

fclose($fp);
?>

SixThreeOh 15-Feb-2010 10:04

If you're bugged by http://bugs.php.net/bug.php?id=5889 then you can try this:

Use igbinary-serialize if you don't mind a little overhead.

Or intarray (by the same person) if you don't mind using a slightly experimental package which may have problems sharing data between differently byte/bit ordered architectures.

I don't believe it would be too difficult to shove a serialize function and unserialize function in there if you rip out the code from igbinary for storing numeric arrays with the correct endianess. Looking at `igbinary_serialize32` and `igbinary_unserialize32` in igbinary.c it should be very easy to copy that functionality to intarray.c.

Take away the "<<0" though, that's just stupid :P

Ref: http://opensource.dynamoid.com/

Anonymous 25-Nov-2009 05:18

may be useful

<?php
    public function unpack_str($str, $len) {
        $tmp_arr = unpack("c".$len."chars", $str);
        $out_str = "";
        foreach($tmp_arr as $v) {
            if($v>0) {
                $out_str .= chr($v);
            }
        }
       
        return $out_str;
    }
   
    public function pack_str($str, $len) {       
        $out_str = "";
        for($i=0; $i<$len; $i++) {
            $out_str .= pack("c", ord(substr($str, $i, 1)));
        }
        return $out_str;
    }
?>

Anonymous 21-Aug-2009 08:30

Coder's example is basically an explanation of bindec() and decbin(), not pack() and unpack().

Here's some code to convert a string binary expression into its binary-string equivalent and vice versa.

(Would be even simpler if pack/unpack offered a 'b' format code....)

<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
  if (!is_string($input)) return null; // Sanity check

// Pack into a string
  return pack('H*', base_convert($input, 2, 16));
}

function bstr2bin($input)
// Binary representation of a binary-string
{
  if (!is_string($input)) return null; // Sanity check

// Unpack as a hexadecimal string
  $value = unpack('H*', $input);
 
  // Output binary representation
  return base_convert($value[1], 16, 2);
}

// Returns string(3) "ABC"
var_dump(bin2bstr('01000001 01000010 01000011'));

// Returns string(24) "010000010100001001000011"
var_dump(bstr2bin('ABC'));
?>

Coder 07-Apr-2009 04:54

These two functions allow conversion between binary string and signed integer with possibility to give the bit length.

Usage:
<?php
echo si2bin(-10, 32);
11111111111111111111111111110110
echo si2bin(10, 32);
00000000000000000000000000001010
echo bin2si("11111111111111111111111111110110", 32);
-10
echo bin2si("00000000000000000000000000001010", 32);
10

// signed integer to binary
function si2bin($si, $bits=32)
{
    if ($si >= -pow(2,$bits-1) and $si <= pow(2,$bits-1) )
    {
        if ($si >= 0) // positive or zero
        {
            $bin = base_convert($si,10,2);
            // pad to $bits bit
            $bin_length = strlen($bin);
            if ($bin_length < $bits) $bin = str_repeat ( "0", $bits-$bin_length).$bin;
        }
        else // negative
        {
            $si = -$si-pow(2,$bits);
            $bin = base_convert($si,10,2);
            $bin_length = strlen($bin);
            if ($bin_length > $bits) $bin = str_repeat ( "1", $bits-$bin_length).$bin;
        }
        return $bin;
    }
}

// binary to signed integer
function bin2si($bin,$bits=32)
{
    if (strlen($bin)==$bits)
    {
        if (substr($bin,0,1) == 0) // positive or zero
        {
            $si = base_convert($bin,2,10);
        }
        else // negative
        {
            $si = base_convert($bin,2,10);
            $si = -(pow(2,$bits)-$si);
        }
        return $si;
    }
}
?>

qwanta 16-Oct-2008 07:28

Unlike the PERL pack function, the PHP version does not accept arrays as arguments (see PHP Bugs: #5889).

To get around this I found something like this works:
<?php
$word_data = array(132,457,234,63);
$packet_send = "";
foreach ($word_data as $word) {
    $packet_send = $packet_send.pack("v",$word);   
}
?>

As Pack returns a string, so you can just concatenate them.

Anonymous 05-Jul-2008 04:17

Array pack:
<?php
function pack_array($v,$a) {
 return call_user_func_array(pack,array_merge(array($v),(array)$a));
}
?>

php at nagler-ihlein dot de 08-May-2008 07:26

Be aware of format code H always padding the 0 for byte-alignment to the right (for odd count of nibbles).

So pack("H", "7") results in 0x70 (ASCII character 'p') and not in 0x07 (BELL character)
as well as pack("H*", "347") results in 0x34 ('4') and 0x70 ('p') and not 0x03 and 0x47.

Quis AT spam.to.my.devnull.quis.cx 25-Jan-2008 06:45

<?PHP
function ntohs($port) {
  $b=pack("N", $port);
  return substr($b,2,2);
}
?>

I've spent a number of hours (n>=2) finding how to do this,
it works like the c function 'ntohs', used for eg the socks5 proxy protocol.

dylan at pow7 dot com 05-Sep-2007 04:00

This is how I used pack to convert base2 to base64 since base_convert doesn't support base64
The base conversions don't work for long strings, which is why I convert 1 byte at a time
Hope this helps someone

function base2to64($base2) {
    if ($remainbits = strlen($base2)%8) $base2 .= str_repeat('0',8-$remainbits);
    $base64 = NULL;
    for ($i=0;$i<strlen($base2);$i+=8) $base16 .= sprintf('%02x',bindec(sprintf('%08d',substr($base2,$i,8))));
    return base64_encode(pack('H*',$base16));
}
function base64to2($base64) {
    list($base16) = unpack('H*0',base64_decode($base64));
    $base2 = NULL;
    for ($i=0;$i<strlen($base16);$i++) $base2 .= sprintf('%04d',base_convert(substr($base16,$i,1),16,2));
    return $base2;
}

Chr dot Eichert<moga at mx dot homelinux dot org> 15-Feb-2007 04:21

This is how you can produce a code that is in fact a picture.
(This code is a complete tool, copy it to a file, call it 'somehow.php' and produce your pictures as hexcode).

<!--//  ***Begin of File***  //-->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<input type="file" name="thefile"><input type="submit">
</form>
<?php
$rh = fopen ($_FILES['thefile']['tmp_name'], "r");
$pb = fread($rh, 8192);
fclose($rh);
$pc = bin2hex($pb);
$pd = wordwrap($pc, 76, "\".<br /> \n \"", 1);
echo "<TT>\$hexpic=\""."$pd"."\"\n</TT>;";
?>
<!--//  ***End of File***  //-->

Copy the result in your site code somewhere. For to show the code as a picture you can use something like what dirk (at) camindo de wrote ...

<?php
$hexpic=".......................
.....................";
$data = pack("H" . strlen($hexpic), $hexpic);
header("Content-Type: image/png");
// maybe your is jpeg / gif / png
header("Last-Modified: " . date("r", filectime($_SERVER['SCRIPT_FILENAME'])));
header("Content-Length: " . strlen($data));
echo $data;
?>

have fun!

dirk (at) camindo de 19-Jan-2007 03:22

Work around newsletter tracking:
include a transparent gif (1x1 pixel) with url = track.php and parameters.
track.php has to write the parameters e.g. into a database and provides the gif - using following code:

header("Content-Type: image/gif");
header("Content-Length: 49");
echo pack('H*',
  '47494638396101000100910000000000ffffffff'
 .'ffff00000021f90405140002002c000000000100'
 .'01000002025401003b'
);

Newdawn.dk 22-Mar-2006 04:25

When trying to create a ZIP file using the pack function - I experienced trouble with the "a" code - It converted all chars correct from the std. ASCII charset but not more language specific like ÆøÅ.
It seems that ZIP files do not use the same HEX for these as everything else does.
The fix was a quick workaround but you'll probably get the picture:
function UniHex($str) {
    // æ ø å Æ Ø Å
    //These are simply one HEX code being replaced by another to correct the issue
    $except = array("E6"=>"91","F8"=>"9B","E5"=>"86","C6"=>"92","D8"=>"9D",    "C5"=>"8F");
    for($i = 0; $i < strlen($str); $i++) {
        $hex = bin2hex(substr($str, $i, 1));
        if ($except[strtoupper($hex)])
            $hex = $except[strtoupper($hex)];
        $return .= $hex;
    }
    return $return;
}
And then i replaced an "a100" code with "H".strlen(uniHex($mystring))

This is like i said a quick workaround, but if you find the real reason for this i'd be happy to see it

j.s.hoekstra 13-Mar-2006 08:57

/* Convert float from HostOrder to Network Order */
function FToN( $val )
{
    $a = unpack("I",pack( "f",$val ));
    return pack("N",$a[1] );
}
   
/* Convert float from Network Order to HostOrder */
function NToF($val )
{
    $a = unpack("N",$val);
    $b = unpack("f",pack( "I",$a[1]));
    return $b[1];
}

Patrik Fimml 11-Oct-2005 10:42

You will get the same effect with

<?php
function _readInt($fp)
{
   return unpack('V', fread($fp, 4));
}
?>

or unpack('N', ...) for big-endianness.

19-Feb-2005 11:09

I needed to convert binary values from a file to integers.

Maybe there is something simpler, but the snippets i saw above seemed a little convoluted:

function bin2asc ($binary)
{
    $val = 0;

for ($i = strlen($binary) - 1; $i >= 0; $i--) {
        $ch = substr($binary, $i, 1);
        $val = ($val << 8) | ord($ch);
    }

return $val;
}

This was called like the following from a binary file:

function _readInt($fp)
{
    return bin2asc(fread($fp, 4));
}

Note that the for loop should be reversed for network byte order instead of intel byte order.  Also the conversion will work with any number of bytes, but will happily overflow.

zilinex at yahoo dot com 01-Sep-2004 11:12

a cool function to converrt numbers to Persian numbers(utf-8)
origin: http://www.farsiweb.info/jalali/jalali.phps

function farsinum($str)
{
  $ret = "";
  for ($i = 0; $i < strlen($str); ++$i) {
        $c = $str[$i];
        if( $c >= '0' && $c <= '9' )
                $out .= pack("C*", 0xDB, 0xB0 + $c);
        else
                $ret .= $c;
  }
  return $ret;
}

Jurgen Braam 02-Oct-2003 11:39

take note: if you produce binary files using PHP on multiple platforms, that you use one of the machine-independent pack options.

This means 's' 'S' 'i' 'I' 'd' and 'f' are _EVIL_ :) Took me some time to figure out what my Excel-generator what futzing about :) Turned out the production machine was a Sun Sparc. I develop on my own x86 Linux server.

Hope this helps anyone...
c-ya,
Jurgen

mfisch[at]kaz[dot]com 10-Jul-2001 09:53

If you are trying to do ascii <--> binary conversions like me;
you probably found that unlike the perl pack functions, these wont help too much. Attached are two functions I wrote to accomplish this task.
<br>
function bin2asc ($binary)
{
  $i = 0;
  while ( strlen($binary) > 3 )
  {
    $byte[$i] = substr($binary, 0, 8);
    $byte[$i] = base_convert($byte[$i], 2, 10);
    $byte[$i] = chr($byte[$i]);
    $binary = substr($binary, 8);
    $ascii = "$ascii$byte[$i]";
  }
  return $ascii;
}
<br>
function asc2bin ($ascii)
{
  while ( strlen($ascii) > 0 )
  {
    $byte = ""; $i = 0;
    $byte = substr($ascii, 0, 1);
    while ( $byte != chr($i) ) { $i++; }
    $byte = base_convert($i, 10, 2);
    $byte = str_repeat("0", (8 - strlen($byte)) ) . $byte; # This is an endian (architexture) specific line, you may need to alter it.
    $ascii = substr($ascii, 1);
    $binary = "$binary$byte";
  }
  return $binary;
}
<br>
Im not sure these are the most efficient functions, but surely alot faster than loading up a perl interpreter for every binary conversion =)

plutus at gmx dot de 10-Aug-2000 04:14

Note that the the upper command in perl looks like this:

$binarydata = pack ("n v c*", 0x1234, 0x5678, 65, 66);
In PHP it seems that no whitespaces are allowed in the first parameter. So if you want to convert your pack command from perl -> PHP, don't forget to remove the whitespaces!

pack()函数中类型对应的符号标记相关推荐

  1. sscanf函数中类型不匹配警告引发的BUG和思考

    2019独角兽企业重金招聘Python工程师标准>>> BUG产生背景 项目开发中的在对网络的IP等地址进行协议封装的处理过程中,我使用了如下的一段代码: buff[0] = con ...

  2. error LNK2019: 无法解析的外部符号 “public: __cdecl ...,函数 ...中引用了该符号解决办法

    vs报错输出如下所示: 1>myview.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl CSPReport::CSPReport(vo ...

  3. html中的if函数,if什么意思_if函数中的,,是什么意思

    if函数中的,,是什么意思 逗号用来区分三个参数. IF函数的括号里包括三个参数,参数1(第一个,前)是一个判断式,参数2(两个,,之间)是满足判断式的时候给出的选择A,参数3是不满足条件给出的判断B ...

  4. Python入门之print()函数中sep和end的用法

    一.print()函数 用于打印输出 刚刚做了一个作业,比如说,你要用print语句输入这样的内容 要求是, 1.向外界获取生日的人的名字 2.定义一个变量name存储这个名字 3.最后得出,&quo ...

  5. 第八章 函数中的类型提示

    应该强调的是,Python 仍将是一种动态类型的语言,即使按照惯例,作者也不希望强制类型提示 --Guido van Rossum, Jukka Lehtosalo, and Łukasz Langa ...

  6. 《流畅的Python第二版》读书笔记——函数中的类型注解

    引言 这是<流畅的Python第二版>抢先版的读书笔记.Python版本暂时用的是python3.10.为了使开发更简单.快捷,本文使用了JupyterLab. 本章关注于Python在函 ...

  7. Asp.Net中几种相似数据绑定标记符号的解释及用法

    Asp.Net中几种相似数据绑定标记符号的解释及用法1.<%#... %> 是在绑定控件DataBind()方法执行时被执行,用于数据绑定 如: <%# Container.Data ...

  8. python中pack函数_关于pack()函数的文章推荐10篇 - pack

    php读写二进制文件可以使用pack和unpack函数. 今天要处理一个二进制文件的问题,所以需要用一下,特意了解一下pack的用法,unpack用法与此类似. 简单来说,pack函数就是给一个目标格 ...

  9. Asp.Net中几种相似的标记符号:解释及用法 还有许多细节

    1.Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %&g ...

最新文章

  1. 某程序员大佬北漂16年,从住地下室到身家千万,如今回老家躺平!
  2. 电脑销售技巧_汝州市,如何查询楼盘详细销售数据?官方公布,精确到房间
  3. Django内置的用户认证
  4. 现场故事:从Log4J迁移到Log4J2
  5. 《C和指针》——带副作用的宏参数
  6. java线程中的死锁_Java多线程中的死锁 - Break易站
  7. day20 Python 高阶函数,函数,嵌套,闭包 装饰器
  8. 长肥管道(LFT)中TCP的艰难处境与打法
  9. 2020互联网岳麓峰会“软件再出发论坛”成功举办
  10. freebsd mysql tmp_FreeBSD下安装MySQL与MySQLdb笔记
  11. 传感器技术—压电式传感器(学习笔记八)
  12. 智能制造与大数据平台
  13. 改变蜡笔小新的眼睛颜色(对bmp图像的部分更改)
  14. 门铃C语言设计程序,单片机C语言程序设计:用定时器设计的门铃
  15. 优化机场值机体验之护照阅读器
  16. 如何在Jsp页面加载时候就能执行某个方法
  17. Apache虚拟主机的实现方式
  18. word文档在线编辑推荐超级文档
  19. java常问面试题总结
  20. 安装sulley注意点

热门文章

  1. 文本编辑器第一阶段测试
  2. C++中的const成员函数
  3. python内置函数open_Python的内置函数open()的注意事项
  4. c++ vector 保存指针
  5. cmake之 ADD_LIBRARY()
  6. tensorflow-TFRecord 用法
  7. python 字符串去除中文
  8. java自定义类怎么比大小_实战:Java 扑克牌比较游戏
  9. python 笔记:argparse
  10. 文巾解题 810. 黑板异或游戏