Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Pack/Unpack Tutorial (aka How the System Stores Data)

by pfaut (Priest)
on Jan 06, 2003 at 16:56 UTC ( #224666=perltutorial: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        struct SupplyRequest {
            time_t request_time;    // time request was entered
    ...
            short quantity;         // quantity needed
            short urgent;           // request is urgent
        };
    
  2. or download this
        $rec = pack( "l i Z32 s2", time, $emp_id, $item, $quan, $urgent);
    
  3. or download this
        Offset   Contents (increasing addresses left to right)
             0   160  44  19  62| 41  82   3   0| 98 111 120 101 115  32 1
    +11 102
    ...
                       p   a   p   e   r   c   l   i   p   s
            32     0   0   0   0   0   0   0   0|  2   0|  1   0
                  00  00  00  00  00  00  00  00| 02  00| 01  00
    
  4. or download this
        ($order_time, $monk, $itemname, $quantity, $ignore) =
            unpack( "l i Z32 s2", $rec );
    
  5. or download this
        pack('a8',"hello") produces "hello\0\0\0"
        pack('Z8',"hello") produces "hello\0\0\0"
    ...
        unpack('Z8',"hello\0\0\0") produces "hello"
        unpack('A8',"hello   ") produces "hello"
        unpack('A8',"hello\0\0\0") produces "hello"
    
  6. or download this
        ord(pack('b8','00100110')) produces 100 (4 + 32 + 64)
        ord(pack('B8','00100110')) produces 38 (32 + 4 + 2)
    
  7. or download this
        pack('h4','1234') produces 0x21,0x43
        pack('H4','1234') produces 0x12,0x34
    
  8. or download this
    #!/usr/bin/perl -w
    use strict;
    ...
    DumpScalar($rec);
    $rec = pack('H4',"1234");                # should produce 0x12,0x34
    DumpScalar($rec);
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://224666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2015-07-29 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls