Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5060

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Pack data into binary string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.ignore-user-abort.html">ignore_user_abort</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.php-check-syntax.html">php_check_syntax</a></div>
 <div class="up"><a href="ref.misc.html">Misc. Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.pack" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pack</h1>
  <p class="verinfo">(PHP 4, PHP 5)</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">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>pack</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$args</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Pack given arguments into a binary string according to
   <em><code class="parameter">format</code></em>. 
  </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.html" 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">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">format</code></em></span>
     <dd>

      <p class="para">
       The <em><code class="parameter">format</code></em> string consists of format codes
       followed by an optional repeater argument. The repeater argument can
       be either an integer value or <em>*</em> 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>f</td>
           <td>float (machine dependent size and representation)</td>
          </tr>

          <tr>
           <td>d</td>
           <td>double (machine dependent size and representation)</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-padded string (new in PHP 5.5)</td>
          </tr>

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

         </tbody>
        
       </table>

      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">args</code></em></span>
     <dd>

      <p class="para">
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>

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

 
 <div class="refsect1 changelog" id="refsect1-function.pack-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.0</td>
       <td>
        The &quot;Z&quot; code was added with equivalent functionality to &quot;a&quot; for Perl
        compatibility.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pack-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-3782">
    <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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">"nvc*"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x1234</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x5678</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">65</span><span style="color: #007700">,&nbsp;</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">Notes</h3>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">Note that PHP internally stores <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> values as
    signed values of a machine-dependent size (C type <em>long</em>).   
    Integer literals and operations that yield numbers outside the bounds of the  
    <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type will be stored as <span class="type"><a href="language.types.float.html" 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.html" class="type integer">integer</a></span> type if it were unsigned.
    In systems where the <span class="type"><a href="language.types.integer.html" class="type integer">integer</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.html" class="type integer">integer</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.html" class="type integer">integer</a></span> type has 64-bit size, the <span class="type"><a href="language.types.float.html" 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
    <em>int</em> type (most UNIX-like systems don&#039;t), the only way to  
    use the <em>I</em> pack format in the upper range is to create
    <span class="type"><a href="language.types.integer.html" class="type integer">integer</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">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.unpack.html" class="function" rel="rdfs-seeAlso">unpack()</a> - Unpack data from binary string</span></li>
   </ul>
  </p>
 </div>

 
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.ignore-user-abort.html">ignore_user_abort</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.php-check-syntax.html">php_check_syntax</a></div>
 <div class="up"><a href="ref.misc.html">Misc. Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>