perlmeditation
ikegami
<p>[doc://pack|<c>pack</c>] and [doc://unpack|<c>unpack</c>] are useful tools for generating strings of bytes for interchange and extracting values from such strings respectively. What follows is a table that represents the relevant formats in a convenient form.
<p>
<table border="1">
<tr>
<th rowspan="2">Category
<th rowspan="2" colspan="2">Type
<th colspan="3">Byte Order
<th rowspan="2">Mnemonic
<tr>
<th>Native
<th>Little-Endian (<c><</c>)
<th>Big-Endian (<c>></c>)
<tr>
<td rowspan="8">Fixed-Size<br>Integers
<td rowspan="2">8-bit<br>integer
<td>Unsigned
<td colspan="3"><c>C</c>
<td rowspan="2">"C" for <em>char</em>
<tr>
<td>Signed
<td colspan="3"><c>c</c>
<tr>
<td rowspan="2">16-bit<br>integer
<td>Unsigned
<td><c>S</c>
<td><c>S<</c> or <c>v</c>
<td><c>S></c> or <c>n</c>
<td rowspan="2">"S" for <em>short</em>
<tr>
<td>Signed
<td><c>s</c>
<td><c>s<</c> or <c>v!</c>
<td><c>s></c> or <c>n!</c>
<tr>
<td rowspan="2">32-bit<br>integer
<td>Unsigned
<td><c>L</c>
<td><c>L<</c> or <c>V</c>
<td><c>L></c> or <c>N</c>
<td rowspan="2">"L" for <em>long</em>
<tr>
<td>Signed
<td><c>l</c>
<td><c>l<</c> or <c>V!</c>
<td><c>l></c> or <c>N!</c>
<tr>
<td rowspan="2">64-bit<br>integer
<td>Unsigned
<td><c>Q</c>
<td><c>Q<</c>
<td><c>Q></c>
<td rowspan="2">"Q" for <em>quad</em>
<tr>
<td>Signed
<td><c>q</c>
<td><c>q<</c>
<td><c>q></c>
<tr>
<td colspan="7">
<tr>
<td rowspan="3">Types Used<br>By This Build<br>of <c>perl</c>
<td colspan="2"><c>UV</c> (unsigned integer)
<td><c>J</c>
<td><c>J<</c>
<td><c>J></c>
<td rowspan="2">"J" is related to "I"
<tr>
<td colspan="2"><c>IV</c> (signed integer)
<td><c>j</c>
<td><c>j<</c>
<td><c>j></c>
<tr>
<td colspan="2"><c>NV</c> (floating-point)
<td><c>F</c>
<td><c>F<</c>
<td><c>F></c>
<td>"F" for <em>float</em>
<tr>
<td colspan="7">
<tr>
<td rowspan="14">Underlying<br>C Types for<br>This Build<br>of <c>perl</c>
<td colspan="2"><c>char</c>
<td>—
<td>—
<td>—
<tr>
<td colspan="2"><c>unsigned char</c>
<td>—
<td>—
<td>—
<tr>
<td colspan="2"><c>signed char</c>
<td>—
<td>—
<td>—
<tr>
<td colspan="2"><c>unsigned short int</c>
<td><c>S!</c>
<td><c>S!<</c>
<td><c>S!></c>
<td rowspan="2">"S" for <em><c>short</c></em>
<tr>
<td colspan="2"><c>signed short int</c>
<td><c>s!</c>
<td><c>s!<</c>
<td><c>s!></c>
<tr>
<td colspan="2"><c>unsigned int</c>
<td><c>I!</c> or <c>I</c>
<td><c>I!<</c> or <c>I<</c>
<td><c>I!></c> or <c>I></c>
<td rowspan="2">"I" for <em><c>int</c></em>
<tr>
<td colspan="2"><c>signed int</c>
<td><c>i!</c> or <c>i</c>
<td><c>i!<</c> or <c>i<</c>
<td><c>i!></c> or <c>i></c>
<tr>
<td colspan="2"><c>unsigned long int</c>
<td><c>L!</c>
<td><c>L!<</c>
<td><c>L!></c>
<td rowspan="2">"L" for <em><c>long</c></em>
<tr>
<td colspan="2"><c>signed long int</c>
<td><c>l!</c>
<td><c>l!<</c>
<td><c>l!></c>
<tr>
<td colspan="2"><c>unsigned long long int</c>
<td>—
<td>—
<td>—
<td rowspan="2"></em>
<tr>
<td colspan="2"><c>signed long long int</c>
<td>—
<td>—
<td>—
<tr>
<td colspan="2"><c>float</c>
<td><c>f</c>
<td><c>f<</c>
<td><c>f></c>
<td>"f" for <em><c>float</c></em>
<tr>
<td colspan="2"><c>double</c>
<td><c>d</c>
<td><c>d<</c>
<td><c>d></c>
<td>"d" for <em><c>double</c></em>
<tr>
<td colspan="2"><c>long double</c>
<td><c>D</c>
<td><c>D<</c>
<td><c>D></c>
<td>A bigger <c>double</c>
</table>
<p>For the pointers used by this build of <c>perl</c>, you can use the following:
<c>
use Config qw( %Config );
use constant PTR_SIZE => $Config{ptrsize};
use constant PTR_PACK_FORMAT =>
PTR_SIZE == 8 ? 'Q'
: PTR_SIZE == 4 ? 'L'
: die("Unrecognized ptrsize\n");
</c>
<p>Notes:
<ul>
<li><c><</c> and <c>></c> indicate byte order. The small end of the bracket is at the least significant end of the number. (<c><</c> for little-endian byte order, and <c>></c> for big-endian byte order.) Can't be used with <c>N</c>/<c>n</c> and <c>V</c>/<c>v</c>.
<li>For integers, <c>!</c> signifies using the C types of this build of <c>perl</c>. Exception: <c>N</c>/<c>n</c>/<c>V</c>/<c>v</c>.
<li>For integers, uppercase indicates unsigned, and lowercase indicates signed. Exception: <c>N</c>/<c>n</c>/<c>V</c>/<c>v</c>.
<li><c>N</c> and <c>n</c> are used for <em>network</em> (i.e. internet) byte order (BE), with the uppercase letter being used for the larger bitsize.
<li><c>V</c> and <c>v</c> are used for <em>VAX</em> byte order (LE), with the uppercase letter being used for the larger bitsize.
<li>Using <c>Q/q</c> requires a Perl with 64 bit integers.
<li>Unsupported types:
<ul>
<li><c>unsigned long long int</c> (<c>Q!</c> would be an obvious choice for it.)
<li><c>signed long long int</c> (<c>q!</c> would be an obvious choice for it.)
<li><c>char</c>
<li><c>unsigned char</c> (<c>C!</c> would be an obvious choice for it.)
<li><c>signed char</c> (<c>c!</c> would be an obvious choice for it.)
</ul>
</ul>