downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

php_check_syntax> <ignore_user_abort
Last updated: Fri, 30 Oct 2009

view this page in

pack

(PHP 4, PHP 5)

packPackt Daten in eine Binär-Zeichenkette

Beschreibung

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

Packt die angegebenen Argumente unter Beachtung von format in eine Binär-Zeichenkette.

Die Idee für diese Funktion entstammt Perl. Alle Formatierungs-Anweisungen funktionieren genau wie dort, allerdings fehlen in PHP einige Format-Codes von Perl (z.B. "u").

Beachten sie, dass der Unterschied zwischen vorzeichenlosen und vorzeichenbehafteten Werten nur Einfluss auf die Funktion unpack() hat, wogegen die Funktion pack() bei vorzeichenlosen und vorzeichenbehafteten Format-Codes dasselbe Ergebnis liefert.

Beachten sie auch, dass PHP Ganzzahlwerte intern als vorzeichenbehaftete Werte speichert, deren Größe vom Maschinentyp abhängig ist. Wenn Sie PHP einen vorzeichenlosen Ganzzahlwert geben, der für diese Art der Speicherung zu groß ist, wird er in eine Gleitkommazahl umgewandelt, was oft zu unerwünschten Ergebnissen führt.

Parameter-Liste

format

Die Zeichenkette format besteht aus Format-Codes, gefolgt von einem optionalen Wiederholungs-Argument. Dieses Argument kann ein ganzzahliger Wert sein oder ein * für Wiederholung bis ans Ende der Daten. Bei den Format-Codes a, A, h und H gibt das Wiederholungs-Argument an, wie viele gleiche Zeichen folgen. Im Zusammenhang mit "@" gibt das Wiederholungs-Argument die absolute Position an, ab der das nächste Zeichen steht. Bei allen anderen steht der Wiederholungs-Zähler für die Anzahl der benutzten Daten-Argumente, die in die sich ergebende Binär-Zeichenkette gepackt werden sollen.

Zurzeit sind folgende Formate implementiert:

pack()-Formatzeichen
Code Beschreibung
a mit NUL gefüllte Zeichenkette
A mit Leerzeichen gefüllte Zeichenkette
h Hex-Zeichenkette, unterer Halbwert zuerst
H Hex-Zeichenkette, oberer Halbwert zuerst
c vorzeichenbehaftetes Zeichen
C vorzeichenloses Zeichen
s vorzeichenbehafteter Short-Typ (immer 16 Bit, Byte-Folge maschinenabhängig)
S vorzeichenloser Short-Typ (immer 16 Bit, Byte-Folge maschinenabhängig)
n vorzeichenloser Short-Typ (immer 16 Bit, Byte-Folge Big Endian)
v vorzeichenloser Short-Typ (immer 16 Bit, Byte-Folge Little Endian)
i vorzeichenbehaftete Ganzzahl (Größe und Byte-Folge maschinenabhängig)
I vorzeichenlose Ganzzahl (Größe und Byte-Folge maschinenabhängig)
l vorzeichenbehafteter Long-Typ (immer 32 Bit, Byte-Folge maschinenabhängig)
L vorzeichenloser Long-Typ (immer 32 Bit, Byte-Folge maschinenabhängig)
N vorzeichenloser Long-Typ (immer 32 Bit, Byte-Folge Big Endian)
V vorzeichenloser Long-Typ (immer 32 Bit, Byte-Folge Little Endian)
f Gleitkommazahl (maschinenabhängige Größe und Wiedergabe)
d Double-Typ (maschinenabhängige Größe und Wiedergabe)
x NUL Byte
X geht in der Zeichenkette ein Byte rückwärts
@ NUL-Auffüllung bis zur absoluten Position

args

Rückgabewerte

Gibt die Daten als Binär-Zeichenkette zurück.

Beispiele

Beispiel #1 pack()-Beispiel

<?php
$binaerdaten 
pack("nvc*"0x12340x56786566);
?>

Die sich daraus ergebende Binär-Zeichenkette ist sechs Bytes lang und enthält die Byte-Folge 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.

Siehe auch

  • unpack() - Entpackt die Daten eines Binär-Strings



php_check_syntax> <ignore_user_abort
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
pack
Anonymous
22-Aug-2009 05: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
08-Apr-2009 01: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 04: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
06-Jul-2008 01: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 04: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 03: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
06-Sep-2007 01: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>
16-Feb-2007 01: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 12: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
23-Mar-2006 01: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 05: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 07: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 08: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
02-Sep-2004 08: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 08: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 06: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 01: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!

php_check_syntax> <ignore_user_abort
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites