Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Mini-Tutorial: Formats for Packing and Unpacking Numbers

by ikegami (Pope)
on Jan 06, 2020 at 08:49 UTC ( #11111036=perlmeditation: 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 (>)
Fixed-Size
Integers
8-bit
integer
Unsigned C "C" for char
Signed c
16-bit
integer
Unsigned S S< or v S> or n "S" for short
Signed s s< or v! s> or n!
32-bit
integer
Unsigned L L< or V L> or N "L" for long
Signed l l< or V! l> or N!
64-bit
integer
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
 
Underlying
C Types for
This Build
of perl
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!>
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");

Notes:

  • < 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. N/n and V/v excepted.
  • For integers, uppercase indicates unsigned, and lowercase indicates signed. N/n and V/v excepted.
  • 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.

Replies are listed 'Best First'.
Re: Mini-Tutorial: Formats for Packing and Unpacking Numbers
by xiaoyafeng (Deacon) on Jan 13, 2020 at 06:47 UTC
    Many thanks! It's very useful.




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://11111036]
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2020-11-27 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?