Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

pack and unpack 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.

Category Type Byte Order Mnemonic
Native Little-Endian (<) Big-Endian (>)
Unsigned C "C" for char
Signed c
Unsigned S S< or v S> or n "S" for short
Signed s s< or v! s> or n!
Unsigned L L< or V L> or N "L" for long
Signed l l< or V! l> or N!
Unsigned Q Q< Q> "Q" for quad
Signed q q< q>
Types Used
By This Build
of perl
UV (unsigned integer) J J< J> "J" is related to "I"
IV (signed integer) j j< j>
NV (floating-point) F F< F> "F" for float
C Types for
This Build
of perl
unsigned char
signed char
unsigned short int S! S!< S!> "S" for short
signed short int s! s!< s!>
unsigned int I! or I I!< or I< I!> or I> "I" for int
signed int i! or i i!< or i< i!> or i>
unsigned long int L! L!< L!> "L" for long
signed long int l! l!< l!>
unsigned long long int
signed long long int
float f f< f> "f" for float
double d d< d> "d" for double
long double D D< D> A bigger double

For the pointers used by this build of perl, you can use the following:

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");


  • < and > indicate byte order. The small end of the bracket is at the least significant end of the number. (< for little-endian byte order, and > for big-endian byte order.) Can't be used with N/n and V/v.
  • For integers, ! signifies using the C types of this build of perl. Exception: N/n/V/v.
  • For integers, uppercase indicates unsigned, and lowercase indicates signed. Exception: N/n and V/v.
  • N and n are used for network (i.e. internet) byte order (BE), with the uppercase letter being used for the larger bitsize.
  • V and v are used for VAX byte order (LE), with the uppercase letter being used for the larger bitsize.
  • Using Q/q requires a Perl with 64 bit integers.
  • Unsupported types:
    • unsigned long long int (Q! would be an obvious choice for it.)
    • signed long long int (q! would be an obvious choice for it.)
    • char
    • unsigned char (C! would be an obvious choice for it.)
    • signed char (c! would be an obvious choice for it.)

In reply to Mini-Tutorial: Formats for Packing and Unpacking Numbers by ikegami

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2023-09-22 01:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found