<?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 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.pack.php',
    1 => 'pack',
    2 => 'データをバイナリ文字列にパックする',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'その他の関数',
  ),
  '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' => 'ja',
    '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">データをバイナリ文字列にパックする</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pack-description">
  <h3 class="title">説明</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">
   指定された引数を <code class="parameter">format</code> に基づいて
   バイナリ文字列にパックします。
  </p>
  <p class="para">
   この関数のアイデアは Perl からのものであり、フォーマット指定用の
   コードは Perl と同様に動作します。しかし、中には存在しない書式コードもあります。
   たとえば Perl の &quot;u&quot; は存在しません。
  </p>
  <p class="para">
   符号付及び符号無しの区別は関数 <span class="function"><a href="function.unpack.php" class="function">unpack()</a></span> にのみ
   影響を与えます。関数 <span class="function"><strong>pack()</strong></span> は符号付及び符号無しの
   フォーマットコードのどちらでも同じ結果となることに注意しましょう。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pack-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">format</code></dt>
     <dd>
      <p class="para">
       フォーマット文字列は、
       フォーマットコードの後にオプションの反復指定用引数が続く形式と
       なっています。反復指定用引数として整数値、または入力データの最後まで
       反復を意味する <code class="literal">*</code> のどちらかを指定することができます。
       a, A, h, H の場合、
       反復数はそのデータ引数が取得する文字の数を指定します。反復数が @ の場合、
       次のデータを置く場所の絶対位置を表します。その他の場合、反復数は
       データ引数が使われる数を指定し、結果のバイナリ文字列にパックされます。
      </p>
      <p class="para">
       現在、実装されているものを以下に示します。
       <table class="doctable table">
        <caption><strong><span class="function"><strong>pack()</strong></span> の書式文字</strong></caption>
        
         <thead>
          <tr>
           <th>コード</th>
           <th>説明</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>a</td>
           <td>NUL で埋めた文字列</td>
          </tr>

          <tr>
           <td>A</td>
           <td>空白で埋めた文字列</td></tr>

          <tr>
           <td>h</td>
           <td>十六進文字列、下位ニブルが先</td></tr>

          <tr>
           <td>H</td>
           <td>十六進文字列、上位ニブルが先</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 (常に 16 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>S</td>
           <td>unsigned short (常に 16 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>n</td>
           <td>unsigned short (常に 16 ビット、ビッグエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>v</td>
           <td>unsigned short (常に 16 ビット、リトルエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>i</td>
           <td>signed integer (サイズおよびバイトオーダーはマシン依存)</td>
          </tr>

          <tr>
           <td>I</td>
           <td>unsigned integer (サイズおよびバイトオーダーはマシン依存)</td>
          </tr>

          <tr>
           <td>l</td>
           <td>signed long (常に 32 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>L</td>
           <td>unsigned long (常に 32 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>N</td>
           <td>unsigned long (常に 32 ビット、ビッグエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>V</td>
           <td>unsigned long (常に 32 ビット、リトルエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>q</td>
           <td>signed long long (常に 64 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>Q</td>
           <td>unsigned long long (常に 64 ビット、マシンのバイトオーダー)</td>
          </tr>

          <tr>
           <td>J</td>
           <td>unsigned long long (常に 64 ビット、ビッグエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>P</td>
           <td>unsigned long long (常に 64 ビット、リトルエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>f</td>
           <td>float (サイズおよび表現はマシン依存)</td>
          </tr>

          <tr>
           <td>g</td>
           <td>float (サイズはマシン依存。 リトルエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>G</td>
           <td>float (サイズはマシン依存。 ビッグエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>d</td>
           <td>double (サイズおよび表現はマシン依存)</td>
          </tr>

          <tr>
           <td>e</td>
           <td>double (サイズはマシン依存。 リトルエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>E</td>
           <td>double (サイズはマシン依存。 ビッグエンディアンバイトオーダー)</td>
          </tr>

          <tr>
           <td>x</td>
           <td>NUL バイト</td>
          </tr>

          <tr>
           <td>X</td>
           <td>1 バイト戻る</td>
          </tr>

          <tr>
           <td>Z</td>
           <td>NUL終端(ASCIIZ) 文字列をNUL で埋める</td>
          </tr>

          <tr>
           <td>@</td>
           <td>絶対位置まで NUL で埋める</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">戻り値</h3>
  <p class="para">
   バイナリ文字列を含むデータを返します。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pack-changelog">
  <h3 class="title">変更履歴</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>バージョン</th>
       <th>説明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        この関数は、失敗時に <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返さなくなりました。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="type"><a href="language.types.float.php" class="type float">float</a></span> と <span class="type"><a href="language.types.float.php" class="type double">double</a></span> 型は、ビッグエンディアンとリトルエンディアンを両方サポートしました。
       </td>
      </tr>

      <tr>
       <td>7.0.15, 7.1.1</td>
       <td>
        &quot;e&quot;, &quot;E&quot;, &quot;g&quot; および &quot;G&quot; コードが、float と double のバイトオーダーをサポートするために追加されました。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pack-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 <span class="function"><strong>pack()</strong></span> の例</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>
     この結果のバイナリ文字列の長さは 6 バイト長で、バイト列 0x12, 0x34,
     0x78, 0x56, 0x41, 0x42となります。
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.pack-notes">
  <h3 class="title">注意</h3>
  <div class="caution"><strong class="caution">警告</strong>
    <p class="simpara">フォーマットコード <code class="literal">q</code>, <code class="literal">Q</code>, <code class="literal">J</code>,
    <code class="literal">P</code> は、32ビット版のPHPビルドでは利用できません</p>
  </div>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    PHP は、<span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 型の値を内部的に格納する際に
    サイズがマシン依存の符号付き値を使うことに注意しましょう。<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
    型の範囲外の数値となる整数リテラルや演算結果は <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
    として保持されます。この float 値を integer としてパックする際には
    まず最初に integer 型へのキャストを行います。
    その結果、できあがるバイトパターンは期待通りにはならないかもしれません。
   </p>
   <p class="para">
    この問題にもっとも関連するのが、符号なしの数値で <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
    型で表現できるものをパックする場合です。
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 型のサイズが 32 ビットであるシステムでのキャスト結果は、
    (実装で定義されている標準 C の符号なし型から符号付き型への変換に依存しますが)
    まるで <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> が符号なし整数であるかのような同一のバイトパターンになることがよくあります。
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 型のサイズが 64 ビットであるシステムでは、
    たいていの場合は <span class="type"><a href="language.types.float.php" class="type float">float</a></span> の仮数部のサイズが足りず、
    値の精度を損なわずに保持することができません。
    ネイティブの 64 ビット C <code class="literal">int</code> 型を持つシステム
    (UNIX 系システムのほとんどは持っていません) では、上側の範囲の値で
    パック書式 <code class="literal">I</code> を使うための唯一の方法は、
    希望する符号なし値と同じバイト表現になるような負の
    <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 値を作ることになります。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.pack-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.unpack.php" class="function" rel="rdfs-seeAlso">unpack()</a> - バイナリ文字列からデータを切り出す</span></li>
   </ul>
  </p>
 </div>


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