<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.pack.php',
    1 => 'pack',
    2 => 'Pack data into binary string',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'Misc. Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.ignore-user-abort.php',
    1 => 'ignore_user_abort',
  ),
  'next' => 
  array (
    0 => 'function.php-strip-whitespace.php',
    1 => 'php_strip_whitespace',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/misc/functions/pack.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pack" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pack</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pack</span> &mdash; <span class="dc-title">Pack data into binary string</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.pack-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pack</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Pack given arguments into a binary string according to
   <code class="parameter">format</code>. 
  </p>
  <p class="para">
   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&#039;s &quot;u&quot; format code.
  </p>
  <p class="para">
   Note that the distinction between signed and unsigned values only
   affects the function <span class="function"><a href="function.unpack.php" class="function">unpack()</a></span>, where as
   function <span class="function"><strong>pack()</strong></span> gives the same result for
   signed and unsigned format codes.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.pack-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">format</code></dt>
     <dd>
      <p class="para">
       The <code class="parameter">format</code> string consists of format codes
       followed by an optional repeater argument. The repeater argument can
       be either an integer value or <code class="literal">*</code> 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.
      </p>
      <p class="para">
       Currently implemented formats are:
       <table class="doctable table">
        <caption><strong><span class="function"><strong>pack()</strong></span> format characters</strong></caption>
        
         <thead>
          <tr>
           <th>Code</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>a</td>
           <td>NUL-padded string</td>
          </tr>

          <tr>
           <td>A</td>
           <td>SPACE-padded string</td></tr>

          <tr>
           <td>h</td>
           <td>Hex string, low nibble first</td></tr>

          <tr>
           <td>H</td>
           <td>Hex string, high nibble first</td></tr>

          <tr><td>c</td><td>signed char</td></tr>

          <tr>
           <td>C</td>
           <td>unsigned char</td></tr>

          <tr>
           <td>s</td>
           <td>signed short (always 16 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>S</td>
           <td>unsigned short (always 16 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>n</td>
           <td>unsigned short (always 16 bit, big endian byte order)</td>
          </tr>

          <tr>
           <td>v</td>
           <td>unsigned short (always 16 bit, little endian byte order)</td>
          </tr>

          <tr>
           <td>i</td>
           <td>signed integer (machine dependent size and byte order)</td>
          </tr>

          <tr>
           <td>I</td>
           <td>unsigned integer (machine dependent size and byte order)</td>
          </tr>

          <tr>
           <td>l</td>
           <td>signed long (always 32 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>L</td>
           <td>unsigned long (always 32 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>N</td>
           <td>unsigned long (always 32 bit, big endian byte order)</td>
          </tr>

          <tr>
           <td>V</td>
           <td>unsigned long (always 32 bit, little endian byte order)</td>
          </tr>

          <tr>
           <td>q</td>
           <td>signed long long (always 64 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>Q</td>
           <td>unsigned long long (always 64 bit, machine byte order)</td>
          </tr>

          <tr>
           <td>J</td>
           <td>unsigned long long (always 64 bit, big endian byte order)</td>
          </tr>

          <tr>
           <td>P</td>
           <td>unsigned long long (always 64 bit, little endian byte order)</td>
          </tr>

          <tr>
           <td>f</td>
           <td>float (machine dependent size and representation)</td>
          </tr>

          <tr>
           <td>g</td>
           <td>float (machine dependent size, little endian byte order)</td>
          </tr>

          <tr>
           <td>G</td>
           <td>float (machine dependent size, big endian byte order)</td>
          </tr>

          <tr>
           <td>d</td>
           <td>double (machine dependent size and representation)</td>
          </tr>

          <tr>
           <td>e</td>
           <td>double (machine dependent size, little endian byte order)</td>
          </tr>

          <tr>
           <td>E</td>
           <td>double (machine dependent size, big endian byte order)</td>
          </tr>

          <tr>
           <td>x</td>
           <td>NUL byte</td>
          </tr>

          <tr>
           <td>X</td>
           <td>Back up one byte</td>
          </tr>

          <tr>
           <td>Z</td>
           <td>NUL-terminated (ASCIIZ) string, will be NUL padded</td>
          </tr>

          <tr>
           <td>@</td>
           <td>NUL-fill to absolute position</td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">values</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.pack-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns a binary string containing data.
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.pack-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Versione</th>
       <th>Descrizione</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        This function no longer returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="type"><a href="language.types.float.php" class="type float">float</a></span> and <span class="type"><a href="language.types.float.php" class="type double">double</a></span> types supports both Big Endian and Little Endian.
       </td>
      </tr>

      <tr>
       <td>7.0.15, 7.1.1</td>
       <td>
        The &quot;e&quot;, &quot;E&quot;, &quot;g&quot; and &quot;G&quot; codes were added to enable byte order support for float and double.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pack-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>pack()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$binarydata </span><span style="color: #007700">= </span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">"nvc*"</span><span style="color: #007700">, </span><span style="color: #0000BB">0x1234</span><span style="color: #007700">, </span><span style="color: #0000BB">0x5678</span><span style="color: #007700">, </span><span style="color: #0000BB">65</span><span style="color: #007700">, </span><span style="color: #0000BB">66</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     The resulting binary string will be 6 bytes long and contain
     the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
    </p></div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.pack-notes">
  <h3 class="title">Note</h3>
  <div class="caution"><strong class="caution">Attenzione</strong>
    <p class="simpara">Format codes <code class="literal">q</code>, <code class="literal">Q</code>, <code class="literal">J</code> and <code class="literal">P</code> are not available on 32-bit PHP builds.</p>
  </div>
  <div class="caution"><strong class="caution">Attenzione</strong>
   <p class="para">Note that PHP internally stores <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> values as
    signed values of a machine-dependent size.
    Integer literals and operations that yield numbers outside the bounds of the  
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type will be stored as <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
    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.
   </p>
   <p class="para">The most relevant case is when packing unsigned numbers that would
    be representable with the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type if it were unsigned.
    In systems where the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type has a 32-bit size, the cast
    usually results in the same byte pattern as if the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> were
    unsigned (although this relies on implementation-defined unsigned to signed
    conversions, as per the C standard). In systems where the
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type has 64-bit size, the <span class="type"><a href="language.types.float.php" class="type float">float</a></span> 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
    <code class="literal">int</code> type (most UNIX-like systems don&#039;t), the only way to  
    use the <code class="literal">I</code> pack format in the upper range is to create
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> negative values with the same byte representation
    as the desired unsigned value.
   </p>
  </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.pack-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.unpack.php" class="function" rel="rdfs-seeAlso">unpack()</a> - Unpack data from binary string</span></li>
   </ul>
  </p>
 </div>

 
</div><?php manual_footer($setup); ?>